Delete orders in Magento using a shell script

In a recent post I’ve shown the steps required to delete orders in Magento using a couple of SQL queries. Since this is a common issue when switching from development to staging and finally to production ennvironments it would be handy to have a shell script that takes care of deleting orders in Magento by simply specifying a list of orders to delete, either by order id or order increment id.

One of the problems with installing additional extensions such as Delete Orders in this particular case  as explained in the previous post on deleting orders in Magento is that these extension will overwrite the sales order grid in most of the cases, thus causing conflicts in the code base.

This is why I’ve decided to write up a simply PHP shell script containing the required commands to delete orders in Magento. Although it’s pretty self-explanatory I’m planning to release an extension on Magento Connect. Meanwhile feel free to use the code provided below.

<?php

require '../app/Mage.php';

Mage::app('admin')->setUseSessionInUrl(false);

/**
 * whether $removeOrderIds are specified as order increment ids (e.g. 100000025)
 * or order ids (e.g. 25).
 */
$useOrderIncrementId = true;

/**
 * specify order ids to remove
 */
$removeOrderIds = array(
);

/**
 * Deletes order either by $orderIncrementId or $orderId.
 * @param int $orderId
 * @param boolean $isOrderIncrementId is order increment id
 * @return boolean
 */
function deleteOrder($orderId, $isOrderIncrementId = false) {
 try {

   $order = null;

   // load order either based on order increment id or order id
   if ($isOrderIncrementId) {
     $order = Mage::getModel('sales/order')->load($orderId, 'increment_id');
   } else {
     $order = Mage::getModel('sales/order')->load($orderId);
   }

   if (!$order->getId()) {
     echo 'Order #' . $orderId . ' does not exist (anymore)' . PHP_EOL;
     return;
   }

   // 1. call delete to trigger delete-CASCADE
   $order->delete();

   // 2. manual clean-up for deleted order
   // credits go to DeleteOrders extensions
   $resource = Mage::getSingleton('core/resource');
   $delete = $resource->getConnection('core_read');
   $orderTable = $resource->getTableName('sales_flat_order_grid');
   $invoiceTable = $resource->getTableName('sales_flat_invoice_grid');
   $shipmentTable = $resource->getTableName('sales_flat_shipment_grid');
   $creditMemoTable = $resource->getTableName('sales_flat_creditmemo_grid');
 
   $sql = "DELETE FROM " . $orderTable . " WHERE entity_id = " . $orderId . ";";
   $delete->query($sql);
   $sql = "DELETE FROM " . $invoiceTable . " WHERE order_id = " . $orderId . ";";
   $delete->query($sql);
   $sql = "DELETE FROM " . $shipmentTable . " WHERE order_id = " . $orderId . ";";
   $delete->query($sql);
   $sql = "DELETE FROM " . $creditMemoTable . " WHERE order_id = " . $orderId . ";";
   $delete->query($sql);

   // 3. we are done!
   echo 'Removed order #' . $orderId . PHP_EOL;

   return true;
 } catch (Exception $e) {
   echo "Failed to remove order #" . $orderId . ": "
   . $e->getMessage() . PHP_EOL;
 }

 return false;
}

/**
 * iterate over $removeOrderIds
 */
foreach ($removeOrderIds as $orderId) {
  deleteOrder($orderId, $useOrderIncrementId);
}

echo "DONE!" . PHP_EOL;

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.