dmlab.workStart a project
← /blog

Deleting a Magento website: a broken credit memo grid and orphaned sales sequences

Deleting a website that has orders broke the credit memo grids and left the sales sequence tables of its store views in the database. How to check your store, how to clean up, and the two Mage-OS fixes.

Deleting a website in Magento looks final: the website, its store groups and its store views disappear from Stores > All Stores. Orders, invoices and credit memos of those store views stay, as they should. But two things didn’t cope with that:

  • The credit memo grids broke. Sales > Credit Memos and the Credit Memos tab of an order failed with “The store that was requested wasn’t found”.
  • Sales sequences were left behind. Every store view gets four tables that number its orders, invoices, credit memos and shipments. They stayed in the database after the store view was gone.

The first one was reported as mage-os/mageos-magento2#394 by @jeanmarcos-dev (and upstream as magento/magento2#41750). I found the second one while working on the first.

Does this affect you?

  • The grid error shows up after deleting a website, a store group or a single store view that has credit memos.
  • Leftover sequences pile up after deleting a website or a store group. Deleting a single store view cleans them up.
  • Magento Open Source and Adobe Commerce (2.4-develop) still have both issues. In Mage-OS, the grid fix ships in 4.1.0, and the sequence fix was merged after 4.1.0 and will be in the next release.

To find sequences of store views that no longer exist:

SELECT m.meta_id, m.entity_type, m.store_id, m.sequence_table
FROM sales_sequence_meta m
LEFT JOIN store s ON s.store_id = m.store_id
WHERE s.store_id IS NULL;

To clean them up, back up the database first, then generate the DROP statements, run them, and delete the meta rows (rows in sales_sequence_profile go with them through a foreign key):

SELECT CONCAT('DROP TABLE IF EXISTS `', m.sequence_table, '`;')
FROM sales_sequence_meta m
LEFT JOIN store s ON s.store_id = m.store_id
WHERE s.store_id IS NULL;

-- run the generated statements, then:
DELETE m FROM sales_sequence_meta m
LEFT JOIN store s ON s.store_id = m.store_id
WHERE s.store_id IS NULL;

If your install uses a table prefix, add it to sales_sequence_meta and store. sequence_table already holds the full table name.

And a practical note: if a website has orders, disabling its store views is usually safer than deleting it. Orders keep pointing at a real store, and nothing is left half-cleaned.

The credit memo grid

The sales entity tables, such as sales_creditmemo, have a foreign key to store with ON DELETE SET NULL. The four grid tables don’t, so sales_creditmemo_grid.store_id keeps the id of a store view that no longer exists.

That alone is harmless. The exception came from the price column, Magento\Sales\Ui\Component\Listing\Column\Price. To format an amount, it needs a currency. It takes base_currency_code from the row, and if the row doesn’t have one, loads the row’s store to get its base currency. sales_creditmemo_grid has no base_currency_code column, so every amount on the credit memo grids does that store lookup. The order and invoice grids do have the column, which is why only the credit memo grids broke.

The fix, in #407, falls back to the default store, which is what the column already does when store_id is empty:

try {
    $store = $this->storeManager->getStore($storeId);
} catch (NoSuchEntityException) {
    // The row may belong to a deleted store view
    $store = $this->storeManager->getStore(Store::DEFAULT_STORE_ID);
}

This has a limit. If your websites have different base currencies (catalog price scope set to Website), the amounts of a deleted store view are shown with the default store’s currency symbol. The numbers are right; the symbol may not be. The credit memo itself keeps its base_currency_code in sales_creditmemo, so a fuller fix could join it into the grid. That changes the grid collection for every store, so this PR only stops the crash.

A foreign key on the grid tables looks like the “proper” fix, but it wouldn’t help. Declarative schema adds foreign keys with checks off, so rows that already point to a deleted store would stay as they are and still break the grid. The grids also had foreign keys before, and they were removed on purpose when the sales grids were reworked in 2015. The other way to skip the store lookup, adding base_currency_code to the credit memo grid, was tried upstream (magento/magento2#22123) and reverted as backward-incompatible.

Sales sequences left behind

Each store view has its own sequences, so order numbers don’t collide between stores. For a store view with id N there are four tables, sequence_order_N, sequence_invoice_N, sequence_creditmemo_N and sequence_shipment_N, and four rows in sales_sequence_meta. Each table gets a row per document created in that store.

Magento removes them in SequenceRemovalObserver, which listens to store_delete. That event is dispatched from Store::afterDelete(), so it fires only when a store view is deleted through its own model.

When you delete a website or a store group, its store views are not deleted through the model. MySQL removes them through the foreign key cascade from store_website and store_group, and no PHP code runs for those rows. So store_delete never fires, and the sequences stay. On a real install, deleting a website with one store view left all four tables and all four meta rows.

The fix, in #409, adds an observer on website_delete_before and store_group_delete_before. It reads the store view ids while they still exist, then drops their sequences with the existing DeleteByStore once the delete is committed:

$storeIds = $scope->getStoreCollection()->getAllIds();
if (!$storeIds) {
    return;
}

$scope->getResource()->addCommitCallback(
    function () use ($storeIds) {
        foreach ($storeIds as $storeId) {
            try {
                $this->deleteByStore->execute((int)$storeId);
            } catch (\Throwable $e) {
                // Keep cleaning up the remaining stores
                $this->logger->critical($e);
            }
        }
    }
);

The commit callback matters. DROP TABLE causes an implicit commit in MySQL, so running it inside the delete transaction would commit half of the delete. Deferred until after the commit, it runs only if the website is really gone. If the delete is rolled back, nothing is dropped. Store::afterDelete() and Group::afterDelete() defer their own work the same way.

@marcelmtz reviewed the PR and suggested the try/catch per store. Magento already catches and logs exceptions from commit callbacks, but one failing store would have skipped the rest, so the loop now logs the failure and moves on.

Why not delete the store views one by one?

Another option is to make website deletion delete each store view through its model, so store_delete fires for every listener. In core, SalesSequence is the only module that listens to store_delete, so fixing that listener covers core. Deleting store views one by one would also run all of their delete logic (store reinit, cache cleaning, plugins of installed extensions) for every store view. That’s a bigger change in behaviour than a bug fix should make.

Is the cleanup expensive?

I haven’t measured it on a large store. Per store view, it runs about four DROP TABLE statements and a few small queries. A sequence table has a row per order, invoice, credit memo or shipment of that store, so on a busy store it can hold millions of rows. The number of products doesn’t matter.

On MySQL before 8.0.23, dropping a large InnoDB table scans the buffer pool and can stall a busy server for a moment; 8.0.23 removed that scan. The same drop already happens today when you delete a single store view. And since the cleanup runs after the commit, it doesn’t make the delete transaction longer.

Tests

  • #407: an integration test deletes a website that has a credit memo and renders both credit memo grids. Both fail on main and pass with the fix. A unit test covers the price column fallback.
  • #409: an integration test deletes a website, a store group and a store view, and checks that no sequence tables or meta rows are left. The website and group cases fail on main and pass with the fix. Unit tests cover the commit timing and a failing store.

#407 was merged on November 9, 2026, and #409 on November 30.

Takeaways

  • A foreign key cascade doesn’t fire model events for the child rows it removes. Any cleanup hooked to the delete event of a child entity misses rows that the database removes for you.
  • DDL can’t run inside a MySQL transaction without committing it. Defer it to a commit callback.
  • A missing foreign key isn’t always the bug. Sometimes the fix belongs in the code that reads the data.
  • If a website has orders, disable it rather than delete it.
  • Thanks to @jeanmarcos-dev for the clear report and to @marcelmtz and @rhoerr for the review.