Skip to content

Instantly share code, notes, and snippets.

@mariolopjr
Created November 18, 2015 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariolopjr/cb4529237b3c74d4688d to your computer and use it in GitHub Desktop.
Save mariolopjr/cb4529237b3c74d4688d to your computer and use it in GitHub Desktop.
Delete all tables in a database that match a specific prefix
/*
* Drop all tables with specific prefix
* Based on: http://stackoverflow.com/a/24668848
* Modified for my needs
*
*/
SET @database_name = 'database';
SET @table_prefix = 'prefix%';
SET FOREIGN_KEY_CHECKS = 0;
SET GROUP_CONCAT_MAX_LEN = 100000;
SELECT * FROM (
SELECT CONCAT('DROP TABLE ', GROUP_CONCAT(TABLE_NAME) , ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = @database_name AND TABLE_NAME LIKE @table_prefix
) a INTO @tbls;
PREPARE stmt FROM @tbls;
EXECUTE stmt;
SET FOREIGN_KEY_CHECKS = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment