Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Delete an Order Programmatically in Magento 2?

Summer Nguyen | 3 days ago

How to Delete an Order Programmatically in Magento 2? How to Delete an Order Programmatically in Magento 2?

The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

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!

How to Delete an Order Programmatically in Magento 2?

How to Delete an Order Programmatically in Magento 2?
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 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.

Magento 2 Delete Orders extension by Mageplaza

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.

  • Schedule For:
    • Define the schedule to remove orders every day, every week, or every month.
    • If set daily, the schedule starts automatically by date.
    • If set weekly, the schedule starts automatically every Monday.
    • If set monthly, the schedule starts automatically on the month’s first day.
  • Start Time:
    • Decide the starting time to remove orders.
    • By this time of the day, the schedule will start.
  • Excluded Period:
    • Insert the period to remove orders.
    • For instance, Period = 10, today is March 10th, 2021, all orders generated before March 10th, 2021 will be removed.
  • Order Status: Choose the order status
  • Customer Groups: Select the customer group whose orders will be removed automatically by schedule
  • Shipping Countries:
    • All Countries: Check every order
    • Specific Country: Check orders with shipping addresses (countries selected)
  • Order Total less than: Restrict the order’s maximum value
  • In addition to having orders deleted automatically, admission can click on the Run Manually button to remove specific orders.
  • Notice: Store admins can remove orders by applying the command line php bin/magento order: delete order_id. For instance, if admins want to remove the order whose ID = 14, they can run the command line php bin/magento order: delete 14.

Email Notification

  • Enable: Choose Yes to allow sending emails to admins every time an order is erased (including manual or auto-deletion)
  • Sender: There are five different types of Magento Sender for store admins to select from, including General Contact, Customer Support, Sales Representatives, Custom Email 1, and Custom Email 2.
  • Email Template:
    • Select the default email template to send email notifications about deleted orders to store admins.
    • To change the email template, admins can generate other email templates in Marketing > Email Template.
  • Send To:
    • Enter the email that gets notifications as an order is removed.
    • Emails must be separated by commas (,).


Delete Order

Delete Order Extension for Magento 2

Easy Order Removal and Database Cleanup. Improve Store Performance and Organization

Check it out!


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:

<?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.

3. Use SQL queries to delete all 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.

  • Step 1: Navigate to your PhpMyAdmin
  • Step 2: Apply the following SQL queries to 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 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

Table of content
    Summer

    A data-driven marketing leader with over 10 years of experience in the ecommerce industry. Summer leverages her deep understanding of customer behavior and market trends to develop strategic marketing campaigns that drive brand awareness, customer acquisition, and ultimately, sales growth for our company.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services