How to drop several MySQL tables

Well, today @work, I had to remove several tables in once (+/- 1500 tables) and as a lazy sysadmin, there is no way I spent my time to delete table by table.
So here, a little MySQL trick:

SET SESSION group_concat_max_len = 1000000;

SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT('`',table_schema'`','.','`',table_name,'`')),';')
INTO @droptables
FROM information_schema.tables
WHERE table_schema = 'foo'
AND table_name LIKE '%bar%' ;

PREPARE s1 FROM @droptables;
EXECUTE s1;
DEALLOCATE PREPARE s1;

 

And voila. More or less 4 secondes. Luv’ it.