Synopsis
I would like to delete test orders from my Magento store because they affect the statistics and reports of sales. Would the following code delete all records from:
- sales/order_invoice
- sales/order_invoice_grid
- sales/order_shipment
- sales/order_shipment_grid
- sales/order_creditmemo
- sales/order_creditmemo_grid
- sales/order_payment_transaction
- sales/order
- sales/order_grid
Code Snippet
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('entity_id', (array) $orderIds);
foreach ($orders as $order) {
// XXX - Possible cascade delete?
$order->delete();
}
Or would I need to delete each of the records individually?
Best Answer
As stated in the comments the script you have should work nicely for magento version 1.4+.
In the 1.4 version the sales entities were mapped to flat tables that have constraints so everything should cascade nicely.
For versions prior to 1.4 all the entities related to sales (orders, invoices, shipments and creditmemos) were EAV and kept in the same tables. So nothing cascade.
For version prior to 1.4 you can try this script.
This should work for later versions also but I don't think it's needed.