What are Webhooks in Magento 2
Discover webhooks in Magento 2: a powerful tool to reduce manual workload for business owners. Learn how they work, their key features, and easy configuration tips.
Summer Nguyen | 11-11-2024
If you want to display a product on your website, it is important to test all the processes related to the products. Thus, there will be sample or test orders in your Magento backend, which should be deleted when your site goes live. Or else these orders will be mixed with real orders from your store.
Unfortunately, Default Magento does not allow deleting orders, resulting in database overload and complicated order management.
So, what’s the solution? How to delete an order programmatically in Magento 2?
In this article, we’ll demonstrate some effective ways to remove your unnecessary orders permanently from your Magento 2 store.
Let’s go into details now!
It is essential to remove old or unnecessary orders to ensure the store backend looks clean and has a proper organization. With the help of Magento 2 Delete Orders, store admins can easily update the order grid. All the relevant documents to the orders will be deleted, including invoices, shipments, and credit memos.
Here are some main features of the Magento 2 Delete Orders extension by Mageplaza:
Delete test orders
As you assess a new extension, you’ll need to generate several test orders, making sure that the extension works seamlessly without any problems.
Remove one order or all orders
Within the backend, store owners can choose to remove one order or many orders at the same time. The deleting procedure is easily implemented with some clicks. What admins have to do is to pick up any order they want, then click on the Delete button.
Remove all related invoices, shipments, and credit memos
As the orders are removed, all the related data (such as shipments, invoices, and credit memos) is also deleted simultaneously. This automatic deleting feature saves admins a lot of time and effort.
The first step to removing all your unwanted orders is to install the Magento 2 Delete Orders extension for your store. The extension is totally free. You can experience using the module for your site without worrying about risks.
Next, go to the Admin panel, navigate to Stores > Configuration > Mageplaza > Delete Orders. Then, set Enable = Yes to activate the extension.
Automatic Delete Configuration
An order can only be removed automatically by schedule only when it meets all the conditions of Order Status, Purchase Date, Store View, Customer Group, Order Total, and Shipping Country.
Email Notification
Delete Order Extension for Magento 2
Easy Order Removal and Database Cleanup. Improve Store Performance and Organization
Check it out!You can utilize php script to delete orders manually. 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, 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) {
Then, upload this file to 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
After that, 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).
Next, as you’re ready, let’s navigate to yourwebsite.com/ordermanagement/deleteorder.php to remove unnecessary orders.
Notice: You should backup your database on a regular basis so that you won’t remove wrong orders.
Moreover, you can also erase 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.
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE `sales_order`;
TRUNCATE `sales_order_datetime`;
TRUNCATE `sales_order_decimal`;
TRUNCATE `sales_order_entity`;
TRUNCATE `sales_order_entity_datetime`;
TRUNCATE `sales_order_entity_decimal`;
TRUNCATE `sales_order_entity_int`;
TRUNCATE `sales_order_entity_text`;
TRUNCATE `sales_order_entity_varchar`;
TRUNCATE `sales_order_int`;
TRUNCATE `sales_order_text`;
TRUNCATE `sales_order_varchar`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sendfriend_log`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `wishlist`;
TRUNCATE `log_quote`;
TRUNCATE `report_event`;
ALTER TABLE `sales_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_datetime` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_decimal` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_int` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_text` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_varchar` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
-- lets reset customers
TRUNCATE `customer_address_entity`;
TRUNCATE `customer_address_entity_datetime`;
TRUNCATE `customer_address_entity_decimal`;
TRUNCATE `customer_address_entity_int`;
TRUNCATE `customer_address_entity_text`;
TRUNCATE `customer_address_entity_varchar`;
TRUNCATE `customer_entity`;
TRUNCATE `customer_entity_datetime`;
TRUNCATE `customer_entity_decimal`;
TRUNCATE `customer_entity_int`;
TRUNCATE `customer_entity_text`;
TRUNCATE `customer_entity_varchar`;
TRUNCATE `log_customer`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;
ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `log_customer` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;
-- Now, lets Reset all ID counters
TRUNCATE `eav_entity_store`;
ALTER TABLE `eav_entity_store` AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;
There are several ways to help you eliminate orders in Magento 2. Choose the way that you feel comfortable to implement. If you have some technical skills, you can consider using the method 1 and 2.
Meanwhile, if you do not know a thing about technical knowledge, then the Delete Orders extension for Magento 2 would be a perfect choice. Once the module is added to your store, you can easily delete your unnecessary orders and related documents.
Related Post