How to Delete Orders in Magento 2?

Well, usually during development, you might need to place few orders to test our overall system which needs to be removed before moving the site to production. Natively, Magento doesn’t allows us to delete orders. In this case, you may need some trick. Well, you can use the following method:

1. Using MySQL Queries

  1. Log into MySQL server via CLI or phpMyAdmin/Adminer
  2. Run the following SQL Queries and it will do the job:
SET FOREIGN_KEY_CHECKS=0;

# Clean order history
TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;

# Clean order infos
TRUNCATE TABLE `sales_creditmemo`;
TRUNCATE TABLE `sales_creditmemo_comment`;
TRUNCATE TABLE `sales_creditmemo_grid`;
TRUNCATE TABLE `sales_creditmemo_item`;
TRUNCATE TABLE `sales_invoice`;
TRUNCATE TABLE `sales_invoiced_aggregated`;
TRUNCATE TABLE `sales_invoiced_aggregated_order`;
TRUNCATE TABLE `sales_invoice_comment`;
TRUNCATE TABLE `sales_invoice_grid`;
TRUNCATE TABLE `sales_invoice_item`;
TRUNCATE TABLE `sales_order`;
TRUNCATE TABLE `sales_order_address`;
TRUNCATE TABLE `sales_order_aggregated_created`;
TRUNCATE TABLE `sales_order_aggregated_updated`;
TRUNCATE TABLE `sales_order_grid`;
TRUNCATE TABLE `sales_order_item`;
TRUNCATE TABLE `sales_order_payment`;
TRUNCATE TABLE `sales_order_status_history`;
TRUNCATE TABLE `sales_order_tax`;
TRUNCATE TABLE `sales_order_tax_item`;
TRUNCATE TABLE `sales_payment_transaction`;
TRUNCATE TABLE `sales_refunded_aggregated`;
TRUNCATE TABLE `sales_refunded_aggregated_order`;
TRUNCATE TABLE `sales_shipment`;
TRUNCATE TABLE `sales_shipment_comment`;
TRUNCATE TABLE `sales_shipment_grid`;
TRUNCATE TABLE `sales_shipment_item`;
TRUNCATE TABLE `sales_shipment_track`;
TRUNCATE TABLE `sales_shipping_aggregated`;
TRUNCATE TABLE `sales_shipping_aggregated_order`;

# Clean cart infos
TRUNCATE TABLE `quote`;
TRUNCATE TABLE `quote_address`;
TRUNCATE TABLE `quote_address_item`;
TRUNCATE TABLE `quote_id_mask`;
TRUNCATE TABLE `quote_item`;
TRUNCATE TABLE `quote_item_option`;
TRUNCATE TABLE `quote_payment`;
TRUNCATE TABLE `quote_shipping_rate`;

# Reset indexes (if you want your orders number start back to 1
TRUNCATE TABLE sequence_invoice_1;
TRUNCATE TABLE sequence_order_1;
TRUNCATE TABLE sequence_shipment_1;
TRUNCATE TABLE sequence_creditmemo_1;
# if you are using more than one stores, you should also truncate sequence_invoice_2...x, sequence_order_2...x tables and so on.

SET FOREIGN_KEY_CHECKS=1;

Note: This will reset and remove all your orders, orders history and bestseller data etc. So, run this if you have a backup of the data first or you find the existing data of no use.

Important: Please do not truncate these tables:

  • sales_order_status
  • sales_sequence_meta
  • sales_sequence_profile
  • sales_order_status_label
  • sales_order_status_state

2. Using Magento 2.x Extension

Well, using MySQL might not be ideal for all of the situations. What if you need to delete any particular order(s). Also, using MySQL requires prior technical knowledge of it and might be complex for some.

In this case, It’s recommended to use https://github.com/manishjoy/magento2-delete-orders

It’s an free extension and can be operated from Admin panel itself.

To debug other issue which may arise during development or production, refer this guide.

If you need any more help, feel free to contact me.

Updated: