Magento – Magento 2: how to delete orders from the database

databasemagento2.2.2orders

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.

Best Answer

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