Magento Themes and Magento Extensions

How to delete an order from Magento database

Share

How to delete an order from Magento database

Sometimes, when you have a bad order, then you might want to delete all the data connected to that bad order. So, here is how to do it.

Lets say, your order ID is 102455667

. So goto your phpmyadmin and run this SQL,

SET @increment_id = '102455667';

SELECT *
    FROM `sales_flat_quote_address_item`
    WHERE `sales_flat_quote_address_item`.`quote_address_id` IN (
        SELECT `address_id`
            FROM `sales_flat_quote_address`
            WHERE `sales_flat_quote_address`.`quote_id` IN (
            SELECT `entity_id`
                FROM `sales_flat_quote`
                WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
                )
        );

DELETE
    FROM `sales_flat_quote_shipping_rate`
    WHERE `sales_flat_quote_shipping_rate`.`address_id` IN (
        SELECT `address_id`
            FROM `sales_flat_quote_address`
            WHERE `sales_flat_quote_address`.`quote_id` IN (
            SELECT `entity_id`
                FROM `sales_flat_quote`
                WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
                )
        );

DELETE
    FROM `sales_flat_quote_address`
    WHERE `sales_flat_quote_address`.`quote_id` IN (
        SELECT `entity_id`
            FROM `sales_flat_quote`
            WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
        );

DELETE FROM `sales_flat_quote_item_option`
    WHERE `sales_flat_quote_item_option`.`item_id`  IN (
        SELECT `item_id`
            FROM `sales_flat_quote_item`
            WHERE `sales_flat_quote_item`.`quote_id` IN (
            SELECT `entity_id`
                FROM `sales_flat_quote`
                WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
                )
        );

DELETE FROM `sales_flat_quote_item`
    WHERE `sales_flat_quote_item`.`quote_id` IN (
        SELECT `entity_id`
            FROM `sales_flat_quote`
            WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
        );

DELETE FROM `sales_flat_quote_payment`
    WHERE `sales_flat_quote_payment`.`quote_id` IN (
        SELECT `entity_id`
            FROM `sales_flat_quote`
            WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id
        );

DELETE FROM `sales_flat_quote`
    WHERE `sales_flat_quote`.`reserved_order_id` = @increment_id;

DELETE FROM `sales_order_datetime`
    WHERE `sales_order_datetime`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order`
            WHERE `sales_order`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_decimal`
    WHERE `sales_order_decimal`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order`
            WHERE `sales_order`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_int`
    WHERE `sales_order_int`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order`
            WHERE `sales_order`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_text`
    WHERE `sales_order_text`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order`
            WHERE `sales_order`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_varchar`
    WHERE `sales_order_varchar`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order`
            WHERE `sales_order`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_entity_datetime`
    WHERE `sales_order_entity_datetime`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order_entity`
            WHERE `sales_order_entity`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_entity_decimal`
    WHERE `sales_order_entity_decimal`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order_entity`
            WHERE `sales_order_entity`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_entity_int`
    WHERE `sales_order_entity_int`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order_entity`
            WHERE `sales_order_entity`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_entity_text`
    WHERE `sales_order_entity_text`.`entity_id` IN (
        SELECT `entity_id`
            FROM `sales_order_entity`
            WHERE `sales_order_entity`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_entity_varchar`
    WHERE `sales_order_entity_varchar`.`entity_id`  IN (
        SELECT `entity_id`
            FROM `sales_order_entity`
            WHERE `sales_order_entity`.`increment_id` = @increment_id
        );

DELETE FROM `sales_order_entity`
    WHERE `sales_order_entity`.`increment_id` = @increment_id;

DELETE FROM `sales_order`
    WHERE `sales_order`.`increment_id` = @increment_id;

Leave a Reply

You must be logged in to post a comment.