Sometimes after develop a Magento store and we want to go live we need to delete our test orders and related invoices, credit memos and shippings.

I found a great script in magentobooker.com but I want to update a bit with adding debug option and the possibility for delete all orders in the store instead only one.

Create a php file with name delete_orders.php and upload to magento folder, then execute it with www.yoursite.com/delete_orders.php.

After verifing the data that will be deleted shown in the screen, change in the script the var $debug to false an run the script again.

For end run the script for a third time and if see a message "No orders to delete" the orders are cleaned properly. Now delete this script from Magento installation.

<?php

/** Script for delete all store orders **/

//Change $debug to false for real deletions
$debug = true; //In this mode only shows deletions [Informative].
$mageFilename = './app/Mage.php';
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);


$ordercol = Mage::getModel('sales/order')->getCollection();
if (!$ordercol->count())
    
    die ('No orders to delete');
foreach ($ordercol as $order)
{
    $invoices = $order->getInvoiceCollection();
    foreach ($invoices as $invoice)
    {
        if ($debug)
            echo "invoice {$invoice->getId()} will be deleted<br/>";
        else
            $invoice->delete();
    }
    $creditnotes = $order->getCreditmemosCollection();
    foreach ($creditnotes as $creditnote)
    {
        if ($debug)
            echo "credit {$creditnote->getId()} will be deleted<br/>";
        else
            $creditnote->delete();
    }
    $shipments = $order->getShipmentsCollection();
    foreach ($shipments as $shipment)
    {
        if ($debug)
            echo "shipment {$shipment->getId()} will be deleted<br/>";
        else
            $shipment->delete();
    }

    if ($debug)
        echo 'the Order # ' . $order->getIncrementId() . ' it will be deleted from database.<br/>';
    else
    {
        if ($order->delete())
        {
            echo 'the Order # ' . $order->getIncrementId() . ' has been deleted from database.<br/>';
        } else
        {
            echo 'Deletion failed.<br/>';
        }
    }
}

?>

 

 

The next process is to go to your administration site and refresh statistics under Reports/refresh statistics, selecting all checkboxes and click submit with the option selected 'Refresh lifetime statistics'

Remember to do this only on a copy of your site, never do on live site before make a full backup of your data.