Is there a way to delete just a credit memo?

I have seen some code snippets & extensions that delete whole orders with all linked invoices, shipments & credit memos, but I just want to delete a specific credit memo.


Take a look at the Magento\Sales\Api\CreditmemoManagementInterface::cancel method and at the Magento\Sales\Api\CreditmemoRepositoryInterface::delete methods. First you should get your creditmemo using Magento\Sales\Api\CreditmemoRepositoryInterface::get($id) method. Then use Magento\Sales\Api\CreditmemoManagementInterface::cancel($id) method to cancel it and Magento\Sales\Api\CreditmemoRepositoryInterface::delete(\Magento\Sales\Api\Data\CreditmemoInterface $entity) to delete. In theory your code should looks like this:

class DeleteCreditmemo

    public function __construct(
        \Magento\Sales\Api\CreditmemoManagementInterface $creditmemoManagement,
        \Magento\Sales\Api\CreditmemoRepositoryInterface $creditmemoRepository
    ) {
        $this->creditmemoManagement = $creditmemoManagement;
        $this->creditmemoRepository = $creditmemoRepository;

    public function deleteById($id)
        try {
            $creditMemo = $this->creditmemoRepository->get($id);
        } catch (\Exception $e) {
            // something went wrong


As I see magento team restrict deletion of creditmemos right in their code:

 * Cancel an existing creditmemo
 * @param int $id Credit Memo Id
 * @return bool
 * @throws \Magento\Framework\Exception\LocalizedException
public function cancel($id)
    throw new \Magento\Framework\Exception\LocalizedException(__('You can not cancel Credit Memo'));
    try {
        $creditmemo = $this->creditmemoRepository->get($id);
        foreach ($creditmemo->getAllItems() as $item) {
        $this->eventManager->dispatch('sales_order_creditmemo_cancel', ['creditmemo' => $creditmemo]);
    } catch (\Exception $e) {
        throw new \Magento\Framework\Exception\LocalizedException(__('Could not cancel creditmemo'), $e);
    return true;

So you can reproduce & use same logic as in the method in your own model, but this is not a right way... In any case, I hope this information will be useful to you.

| improve this answer | |
  • I'm really sorry but I'm quite a noob when it comes to Magento so I'm not sure what to do with this code. Should I create a module and add this code inside and run it? – Imesh Liyanage Aug 10 '17 at 3:55
  • @ImeshLiyanage Sure, you should create own module with a controller/view. It's hard to say what is better way, but feel free to ask another question if you have :) – Siarhey Uchukhlebau Aug 10 '17 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.