Magento 1.9 – Delete all orders before a specific date

Before writing this article, I searched many ways through Google. The basic ideas of these methods are correct, but there are more or less problems and cannot achieve the purpose of deleting orders. So I modified and tested the following code, which is completely feasible.

Delete all orders before a specific date

For example, I need to delete all older orders from 2017-01-30 00:00:00 and 2020-05-30 23:59:00.

Step 1: delete orders, invoices, credit notes, payment references, quote

<?php
require_once 'app/Mage.php';

// run application as admin
Mage::app('admin')->setUseSessionInUrl(false);
//or use Mage::register('isSecureArea', true);


/*
Find orders for a given time interval

$dateFrom = '2017-01-30';
$dateTo = '2020-01-30';
 
$fromDate = Mage::app()->getLocale()->date($dateFrom)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
echo $fromDate."<br>";
$toDate = Mage::app()->getLocale()->date($dateTo)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
echo $toDate."<br>";
*/

//$orders = Mage::getModel('sales/order')->getCollection();  
/* show all orders
, for test */

$from = new DateTime('2017-01-30 00:00:00');
$from = $from->format('Y-m-d H:i:s');

$to = new DateTime('2020-05-30 23:59:00');
$to = $to->format('Y-m-d H:i:s');

$orderCollection = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('created_at', array('from'=>$from, 'to'=>$to))
    ->addAttributeToFilter('status', array('neq' => Mage_Sales_Model_Order::STATE_NEW));

echo "The number of orders queried is ".$orderCollection->count()."<br>";


foreach ($orders as $order) {
    echo $order->getIncrementId()."<br>";
    //delete invoices
    $invoices = $order->getInvoiceCollection();
    foreach ($invoices as $invoice) {
        //invoice items will be deleted as well
        $invoice->delete();
    }
    //delete invoices
    $shipments = $order->getShipmentsCollection();
    foreach ($shipments as $shipment) {
        //shipment items will be deleted as well
        $shipment->delete();
    }
    //delete credit notes
    $creditMemos = $order->getCreditmemosCollection();
    foreach ($creditMemos as $creditMemo) {
        //credit note items will be deleted as well
        $creditMemo->delete();
    }
    //delete payment references
    $payments = $order->getPaymentsCollection();
    foreach ($payments as $payment) {
        $payment->delete();
    }
    //delete quote
    $quote = Mage::getModel('sales/quote')->load($order->getQuoteId());  
    if ($quote->getId()) {
        foreach ($quote->getAllItems() as $item) {
            $item->delete();
        }
        $quote->delete();
    }
    //delete the order, items will be deleted as well. So does addresses.
    $order->delete();
}

echo 'The end!';

Reference article:

An advertisement is inserted here. If you need to buy a Windows 10 key, you can purchase it through the following link:

Step 2: Run SQL Query

If you have run the script above and deleted the orders, but it is still seen in the Orders Grid (even after you have refreshed the cache), the problem might be caused by the foreign key. The foreign key in the sales_flat_order_grid may not be working

You will find that the order information is still displayed in the magento backend, like Sales -> Orders, Sales -> Invoices, and so on

//   DELETE FROM `sales_flat_order_grid` WHERE `created_at` < "2020-05-30"
/*If you have deleted the order, but it is still seen in the Orders Grid (even after you have refreshed the cache), the problem might be caused by the foreign key. The foreign key in the sales_flat_order_grid may not be working
*/


//   DELETE FROM `sales_flat_invoice_grid` WHERE `created_at` < "2020-05-30"

Reference article:

https://community.magento.com/t5/Magento-1-x-Programming/How-to-Remove-Deleted-Orders-From-admin-Sales-Order/td-p/31195

webmasters