Magento: How to Delete a credit memo
If you find yourself in the unfortunate position of having to delete a credit memo or order in Magento then take a deep breath and keep reading. I found my self in this very position. At first I was just going to run some sql to remove the orders and credit memo, but that started getting pretty messy. So Below is my solution for deleting an order and Deleting a credit memo attached to an order(but not deleting the order).
Before proceeding Please Note: This was written using Magento Version 1.4.1.1. Because of the vast differences between version this may or may not work on other version. Test anything on a staging site before trying to apply to a production site.
To run the following code snippets you need to add them to an Admn controller action, then go to this action in the admin area. What controller you temporarily add this to is your choice.
//Delete two orders
/* */
$orders = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('increment_id', array('in', array('XXX000700', 'XXX000700-1')));
if(count($orders) delete();
}
} else {
Die("incorect number of orders to delete.");
}
Now We Delete a Credit Memo we did not want
//Now Delete the Refund for an order but do not delete the order.
$order = Mage::getModel('sales/order')->loadByIncrementId('XXX000320');
//Delete Credit Memo
$creditMemos = $order->getCreditmemosCollection();
foreach($creditMemos as $cm){ //cancel each credit memo for the order
$state = $cm->getState();
if($state == 3){//Cancled
continue;
}
$cm->cancel()
->save()
->getOrder()->save(); //Needed to save the order to apply the canceled credit memo to all order items.
$cm->delete();
}
After running the above code I did need to edit the refund values for the order in this table: sales_flat_order
when canceling the credit memo it changed the offline refund amount instead of the online refund amount.

2 Responses to “ Magento: How to Delete a credit memo ”
September 7th, 2011 at 2:37 pm
Sorry to ask this, but how do you add those snippets to an admin controller action ? I’m currently mucking about in phpmyadmin to fix this, and I would like to use this solution instead of searching in tables…
September 7th, 2011 at 2:46 pm
You can add them to any admin controller action. The code just needs to be in a Admin controller because one of the objects checks to see that the request was made from the admin and not the frontend.
This is only a quick fix and If you happen to need to do this alot then I would invest in building out a more robust solution.
NOTE: I do not recommend modifying core files, but if you add this code then remove it once you have ran it. you should be ok.
But to answer you question,
This is the directory that contains all the Magento core controllers. Just choose one and past the code into it
/app/code/core/Mage/Adminhtml/controllers/…
I might choose: /app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php
and add the code to the “indexAction”. This is the action that fires when you go to Admin -> Catalog -> Manage Products.