For testing purposes, there are numerous test orders that we create. Now, once the eCommerce store is live and you start getting the orders, too many orders can create a mess. It is important that you test all the processes related to products.
Unfortunately, default Magento 2 does not offer this feature of deleting orders that result in database overload and complicated order management.
To help you with this, we have brought some effective ways to delete the unnecessary orders permanently from the Magento 2 Store. Here is a PHP script that can help you delete orders manually in Magento 2 store.
Go to the file manager on your server and generate a deleteorder.php file with the command below:
<?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, separate 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 the below-mentioned syntax:
foreach(range(10000001, 3000888) as $id) {
Now, upload the file on your web store.
You should put it into a folder to prevent system exploits. For instance, you place deleteorder.php file in /ordermanagement folder, so the path to this file will appear:
ordermanagement/deleteorder.php
Now, navigate to Magento 2 Dashboard > Sales > Order and search for the IDs of orders you want to eliminate from Magento 2 and replace those IDs with $ids = array (id1, id2, id3, id4, id5, id6).
There you go, It is done. There are many more methods to delete the order programmatically, stay tuned to know more about it. Hope this procedure helped you get rid of unnecessary orders in your Magento 2 Store.
If you face any issue, feel free to reach us out. Our Magento 2 Experts will be happy to help.