<?php
//http://archive.sharpdotinc.com/magento-how-to-delete-a-credit-memo/
//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();
}