Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created September 19, 2024 06:58
Show Gist options
  • Save peterjaap/8b3682aa99a044d5d4ca46511a03f25e to your computer and use it in GitHub Desktop.
Save peterjaap/8b3682aa99a044d5d4ca46511a03f25e to your computer and use it in GitHub Desktop.
Query to remove Magento 2 sequence tables for non-existing stores
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.`', TABLE_NAME, '`') INTO @tables FROM (
SELECT *
FROM information_schema.tables AS t
WHERE t.table_schema = 'DB NAME'
AND t.table_name REGEXP "^sequence_(.+)_[0-9]+"
AND SUBSTRING_INDEX(t.table_name, '_', -1) NOT IN (SELECT DISTINCT store_id FROM sales_sequence_meta)
) TT;
SET @tables = CONCAT('DROP TABLE ', @tables);
SELECT @tables;
PREPARE stmt1 FROM @tables;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment