📜  magento 2 order delete from db - SQL (1)

📅  最后修改于: 2023-12-03 15:32:47.562000             🧑  作者: Mango

Magento 2 Order Delete from DB - SQL

In Magento 2, deleting an order from the database is not a simple task. The process involves deleting multiple records from multiple tables in a specific order to avoid data inconsistencies. In this guide, we will discuss how to delete an order from the Magento 2 database using SQL commands.

Prerequisites

Before proceeding with this guide, you should have the following:

  • Full access to the Magento 2 database
  • Knowledge of SQL commands and database management
Steps to Delete an Order

Follow the below steps to delete an order from the Magento 2 database:

  1. Determine the order ID that you want to delete. You can find the order ID by navigating to the Sales > Orders menu in the Magento 2 admin panel.

  2. Open a SQL client of your choice and connect to the Magento 2 database.

  3. Execute the following SQL command to delete the order from the sales_order table:

    DELETE FROM sales_order WHERE entity_id = <order_id>;
    

    Replace <order_id> with the ID of the order you want to delete.

  4. Execute the following SQL commands to delete the order's associated data from the following tables:

    DELETE FROM sales_order_address WHERE parent_id = <order_id>;
    DELETE FROM sales_order_grid WHERE entity_id = <order_id>;
    DELETE FROM sales_order_item WHERE order_id = <order_id>;
    DELETE FROM sales_order_payment WHERE parent_id = <order_id>;
    DELETE FROM sales_order_status_history WHERE parent_id = <order_id>;
    DELETE FROM sales_order_tax WHERE order_id = <order_id>;
    DELETE FROM sales_order_tax_item WHERE tax_id IN (SELECT tax_id FROM sales_order_tax WHERE order_id = <order_id>);
    

    Replace <order_id> with the ID of the order you want to delete.

  5. Execute the following SQL command to delete the order's associated data from the quote tables:

    DELETE FROM quote WHERE entity_id IN (SELECT quote_id FROM sales_order WHERE entity_id = <order_id>);
    DELETE FROM quote_address WHERE quote_id NOT IN (SELECT quote_id FROM sales_order) AND quote_id IN (SELECT quote_id FROM sales_order_address WHERE parent_id = <order_id>);
    DELETE FROM quote_item WHERE quote_id NOT IN (SELECT quote_id FROM sales_order) AND quote_id IN (SELECT quote_id FROM sales_order_item WHERE order_id = <order_id>);
    DELETE FROM quote_item_option WHERE item_id IN (SELECT item_id FROM quote_item WHERE quote_id NOT IN (SELECT quote_id FROM sales_order) AND quote_id IN (SELECT quote_id FROM sales_order_item WHERE order_id = <order_id>));
    DELETE FROM quote_payment WHERE quote_id NOT IN (SELECT quote_id FROM sales_order) AND quote_id IN (SELECT quote_id FROM sales_order_payment WHERE parent_id = <order_id>);
    DELETE FROM quote_shipping_rate WHERE address_id IN (SELECT address_id FROM quote_address WHERE quote_id NOT IN (SELECT quote_id FROM sales_order) AND quote_id IN (SELECT quote_id FROM sales_order_address WHERE parent_id = <order_id>));
    DELETE FROM report_event WHERE object_id = <order_id> AND object_type = 'order';
    

    Replace <order_id> with the ID of the order you want to delete.

  6. Execute the following SQL command to delete the order's association with the customer:

    DELETE FROM customer_order WHERE order_id = <order_id>;
    

    Replace <order_id> with the ID of the order you want to delete.

Congratulations! You have successfully deleted an order from the Magento 2 database using SQL commands. It is important to note that this process should only be used as a last resort and should be performed with caution. Always backup your database before making any changes.