Magento 2 Delete test orders
If you want to display a product on your website, it’s important to test all the processes related to the products. This means creating sample or test orders in your Magento backend, which should be deleted when your site goes live. Otherwise, these orders may get mixed up with real orders from your store.
Unfortunately, default Magento does not allow for the deletion of orders, leading to database overload and complicated order management.
So, what’s the solution? How can you delete an order programmatically in Magento 2?
In this article, we’ll demonstrate some effective ways to permanently remove unnecessary orders from your Magento 2 store.
How to Delete an Order Programmatically in Magento 2?
1. Use an extension
It is essential to remove old or unnecessary orders to ensure the store backend looks clean and is properly organized. With the help of Magento 2 Delete Orders, store admins can easily update the order grid. All relevant documents associated with the orders will be deleted, including invoices, shipments, and credit memos.
Here are some key features of the Magento 2 Delete Orders extension by Mavenbird:
Delete Test Orders: When testing a new extension, you may need to create multiple test orders to ensure everything functions smoothly without any issues.
Remove One Order or Multiple Orders: Store owners can easily choose to delete either a single order or several orders at once through the backend. The deletion process is straightforward and can be completed with just a few clicks. Admins simply select the desired order(s) and click the Delete button.
Remove All Related Invoices, Shipments, and Credit Memos: When orders are deleted, all associated data, including shipments, invoices, and credit memos, are also automatically removed. This feature saves admins significant time and effort.
The first step in removing unwanted orders is to install the Magento 2 Delete Orders extension for your store. This extension is completely free, allowing you to use it without any risk.
Next, navigate to the Admin panel, go to Stores > Configuration > Mavenbird > Delete Orders, and set Enable = Yes to activate the extension.
Automatic Delete Configuration: Orders can only be removed automatically on a schedule if they meet specific criteria, including Order Status, Purchase Date, Store View, Customer Group, Order Total, and Shipping Country.
- Schedule For:
- Define the frequency for removing orders: daily, weekly, or monthly.
- If set to daily, the schedule begins automatically by date.
- If set to weekly, the schedule starts every Monday.
- If set to monthly, the schedule starts on the first day of each month.
- Start Time: Determine the time of day when the order removal schedule will begin.
- Excluded Period: Specify a period for order removal. For example, if the period is set to 10 and today is March 10, 2021, all orders created before this date will be deleted.
- Order Status: Select the order status for which orders will be removed.
- Customer Groups: Choose the customer group whose orders will be automatically removed according to the schedule.
- Shipping Countries:
- All Countries: Include every order.
- Specific Country: Include only orders with shipping addresses in the selected countries.
- Order Total less than: Set a maximum order value limit for deletion.
- Store admins can also manually delete specific orders by clicking the Run Manually button.
- Command Line Option: Admins can remove orders using the command line:
php bin/magento order:delete order_id
. For example, to delete the order with ID = 14, runphp bin/magento order:delete 14
.
Email Notification
- Enable: Select "Yes" to permit email notifications to admins whenever an order is deleted, whether manually or automatically.
- Sender: Choose from five different Magento sender options for store admins, including:
- General Contact
- Customer Support
- Sales Representatives
- Custom Email 1
- Custom Email 2
- Email Template: Pick the default email template for notifying store admins about deleted orders. Admins can create additional email templates by navigating to Marketing > Email Template.
- Send To: Provide the email addresses that will receive notifications when an order is removed. Make sure to separate multiple emails with commas (,).
2. Use php script
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:
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) {
Next, upload the file to your web store, placing it in a designated folder to prevent potential system exploits. For
example, you can store the deleteorder.php
file in the /ordermanagement
folder, resulting
in the following path:
ordermanagement/deleteorder.php
After that, go to the Magento 2 Dashboard and navigate to Sales > Order. Search for the IDs of the orders you want to delete from Magento 2 and replace those IDs in the code with:
$ids = array (id1, id2, id3, id4);
Once you’re ready, visit yourwebsite.com/ordermanagement/deleteorder.php
to remove the unnecessary
orders.
Note: It's essential to regularly back up your database to avoid accidentally deleting the wrong orders.
3. Use SQL queries to delete all orders
Additionally, you can delete orders directly from the database using SQL queries. However, be cautious with this method, as it will remove all orders, order history, invoices, shipments, credit memos, and quote products from the database. You won't have the option to select specific orders for deletion, so proceed with care.
- Step 1: Access your PhpMyAdmin.
- Step 2: Execute the following SQL queries on your database:
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;
Conclusion
There are multiple methods available for removing orders in Magento 2. You can choose the approach that you feel most comfortable with. If you possess some technical skills, consider using methods 1 and 2.
On the other hand, if you lack technical knowledge, the Delete Orders extension for Magento 2 would be an ideal option. Once you add the module to your store, you can effortlessly delete unnecessary orders and their associated documents.