Different way to Delete an Order in Magento 2

Delete an Order in Magento 2

We can delete or truncate orders data by two methods

  • By PHP script.
  • By SQL query.

Step 1: Use PHP Script

For deleting or truncating order by Script follow the steps below

  • Go to File Manager on your server. You can also connect to file manager with FileZilla or any others file transferring software.
  • Navigate to Magento Root Directory.
  • Create a file <FILE NAME>.php e.g. deleteorder.php.
  • Add the below script and save.
<?php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$registry = $objectManager->get('Magento\Framework\Registry');
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$ids = array(00123,00456,000453,0002544); // insert your order IDs here, separte by comma
foreach ($ids as $id) {
   $order = $objectManager->create('Magento\Sales\Model\Order')->load($id);
   $registry->register('isSecureArea','true');
   $order->delete();
   $registry->unregister('isSecureArea');
   echo "order deleted";
}

If you want to mass remove orders in ranges, apply this syntax

foreach(range(1000000010, 4000000999) as $id) {

You should put it into a folder to prevent system exploits. For instance, you place deleteorder.php file in ordermanagement folder, so the path will be

ordermanagement/deleteorder.php

Next, navigate to yourwebsite.com/ordermanagement/deleteorder.php to remove unnecessary orders.

Step 2: Use SQL Queries to delete all orders

You can also delete orders from the database by using SQL queries. Keep in mind that using this method will remove all the orders. order history, invoices, shipments, credit memos, quote products from the database. You cannot choose specific orders to delete. Therefore, you need to be careful when choosing this method.

  1. Navigate to your PhpMyAdmin
  2. Apply the following SQL queries to your database
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE `gift_message`;
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`;
TRUNCATE TABLE `reporting_orders`;
TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;
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`;
TRUNCATE TABLE `tax_order_aggregated_created`;
TRUNCATE TABLE `tax_order_aggregated_updated`;
SET FOREIGN_KEY_CHECKS=1;

Note: I recommend to take database backup before deleting or truncating the orders.

Please contact us at manish@bay20.com or call us at +91-8800519180 for any support related to Magento 2. You can also visit the Magento2 development page to check the services we offer.