As a store owner, sometimes you may want to delete orders or related invoices, shipments, credit memos. Unfortunately, Magento 2 does not include delete
action in order management section
In order management, you can only Cancel/Hold/Unhold orders. There is no option to completely remove orders from the database.
In this tutorial, I will guide you on how to completely delete orders, test orders, invoice, shipments, credit memos from Magento 2 database with 3 working methods. You can choose the method that best works for you.
Contents
Method 1: using free extension (recommended)
Previously at the beginning of Magento 2 era, there’s no extension to enable delete orders from Magento 2 database. However, as the Magento 2 community keeps growing, there are many 3rd party extension providers who bring valuable contributions to the community.
Nowaday, there are many free Magento 2 extensions that enable delete orders function in Magento 2. We simply just need to install the extension and the delete function will be available in Order management section, and you can even mass select orders, invoice or shipments.
In this article, I will introduce the 3 most trusted extensions. All of them are free, light-weighted, and easy to install.
Magento delete order extension by Mageplaza (free)
>Download link: Download from Github | Download from Mageplaza
This free Magento 2 extension allows admin to delete test orders, cancelled orders, invoices, shipments, credit memos.
The extension simply works like this: it adds delete
option to Action dropdown menu in orders management section.
How to install this extension
This extension is free, you can install this extension using Composer or manually install with .zip file.
To install Mageplaza delete order extension via Composer run the following command
composer require mageplaza/module-delete-orders php bin/magento setup:upgrade php bin/magento setup:static-content:deploy
Now go to Admin > Store > Mageplaza Extensions > Delete orders and choose yes
to enable the extension
Now the delete order function is enabled, you can use this function in order management section.
Select the orders you want to delete and click on Actions dropdown menu, then select Delete
to completely remove the selected orders from Magento 2 database.
You can do the same with invoices, shipments, credit memos.
Magento delete order extension by Magezon (free)
>Download link: Download from Magezon
Another great extension that enable delete order function in Magento 2 is Magezon Delete Orders extension.
The extension also adds an extra option to delete order to action dropdown menu,
You can also delete individual order in order view page
One thing that I love about Magezon extension is that we can assign deleting permission to specific users. This is very helpful if your store has multiple admins, you can choose who is eligible to delete orders, who not.
App
folder. Upload this to Magento 2 root folder on your server and run the following command to update databasephp bin/magento setup:upgrade
Finally, go to store > Configuration > MAGEZON EXTENSIONS > Delete order to enable the extension. Now the extension should be available to use at order management section.
Magento delete order extension by Bsscommerce (free)
>Download link: Download from Bsscommerce
Magento 2 delete order extension by Bsscommerce enable delete order/ delete invoice/ delete shipment option in admin section.
The difference between Bsscommerce and other extensions is that it separates delete shipment and delete invoice from delete order function
How to install this extension
You can download extension package here and manually install it by uploading extension folder to Magento 2 root folder.
Method 2: using php script
If you do not want to install 3rd party extension to enable delete order function, you can use php script to manually delete orders.
Open file manager on your server and create a deleteorder.php
file with the following content
<?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"; }
In case you want to mass delete orders in range, use this syntax
foreach(range(1000000010, 4000000999) as $id) {
Next, upload this file to your web site. I suggest to put it into a folder to avoid system exploit.
For example, I put deleteorder.php file in /ordermanagement
folder, so the path to this file will be: ordermanagement/deleteorder.php
Next, go to Magento 2 Dashboard > Sales > Order and look for ids of the orders you want to delete from Magento 2 and replace those ids with $ids = array(id1,id2,id3,id4);
Finally, when you are ready go to yourwebsite.com/ordermanagement/deleteorder.php to delete unwanted orders
Note: Remember to backup your database regularly so that you won’t delete wrong orders.
Method 3: using SQL queries to remove all orders
You can also delete orders from database by using SQL queries (I do not recommend this method unless you know what you are doing!)
Go to phpmyadmin, select Magento 2 website database and navigate to SQL tab to enter SQL querries
To Clean order history
To clean order information
To clean cart information
To reset indexes (reset the number of orders stored in database to 1)
Note, the shortage of using SQL queries method is that it will delete all the orders, order history, shipments, invoices, credit memos, also the quotes, quote items from database, you can’t select specific orders so please be careful when using this method.
Always do a regular backup of your website
Wrapping up
Magento 2 is still lacking a lot of features for store owners. Luckily, as an opensource platform, we managed to integrate more features to Magento 2 using 3rd party extensions or some custom code.
Deleting Magento 2 orders function should be available as a built-in feature in the next releases of Magento 2 so that store owners don’t need to look up for other solutions.
If you have any problems when deleting orders in Magento 2, please drop a comment below and I will be happy to help.