Since this is a typical issue when changing from improvement to organizing lastly to creation ennvironments it is convenient to have a shell script that deals with erasing orders in Magento by essentially indicating a rundown of requests to erase, either by request id or request increment id.
In a late post I've demonstrated the strides required to erase orders in Magento utilizing several SQL inquiries. Since this is a typical issue when changing from improvement to organizing lastly to creation ennvironments it is convenient to have a shell script that deals with erasing orders in Magento by essentially indicating a rundown of requests to erase, either by request id or request increase id.
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; ?>