8

How can I delete the test orders created in my store? I have opened the database but I couldn't find the order table. Please help me to delete the orders. I'm using the later version of Magento 2.

6

I suggest you to avoid direct mess with SQL.

You can use any good extension like Mageplaza

Another option is to create a script on root and delete order programmatically

You can create a file at root with following code:

<?php

ini_set('error_reporting', E_ALL);
ini_set("display_errors", "1");

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(1,2,3,4); // your order_id 

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";

}

Edit I

If you want to delete order using script you can put above code on root folder of magento.After that you can hit URL in browser.

For example your magento is installed at www.example.com and your file name is deleteOrder.php you can run it by:

 www.example.com/deleteOrder.php

If you want to install extension you need to

- Extract folder at [magentoRoot]/app/code
- Open terminal and run cd [magentoRoot] //change to root dir
- php bin/magento setup:upgrade
- php bin/magento cache:flush
- php bin/magento setup:static-content:deploy (only required in production mode)

You can also find document at Official Site

  • But that is not free, right? – Elisha Senoo Sep 14 '18 at 17:24
  • Module I shared is free, if you don't want to go with module simply use one time script. – Pawan Sep 14 '18 at 17:32
  • because once we delete dummy order, we will rarely use that extension. – Pawan Sep 14 '18 at 17:33
  • Welcome @ElishaSenoo. Are you going with module Or script ? – Pawan Sep 14 '18 at 17:50
  • I am going with module for now. – Elisha Senoo Sep 14 '18 at 17:52
4

You can delete all the orders, order history, shipments, invoices, credit memos, also the quotes, quote items from database by following SQL:

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;


SET FOREIGN_KEY_CHECKS=1;

Before doing above take a backup of your database.

Hope this will help.

  • What if I want to delete particular orders, not all? – Elisha Senoo Sep 14 '18 at 17:22
  • You check the entity id of those and delete those accordingly. Also if you want to use extension then you can try github.com/mageplaza/magento-2-delete-orders – Sukumar Gorai Sep 14 '18 at 17:26
  • I hope the extension is free? – Elisha Senoo Sep 14 '18 at 17:47
  • Extension url is from GitHub. So it's free to download. – Sukumar Gorai Sep 14 '18 at 17:49
  • I saw that, I just wanted to be sure. Thanks – Elisha Senoo Sep 14 '18 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.