Drop multiple tables from a MySQL database

by Jordie on August 11th, 2009

Problem:

I want to remove a set of tables from the database quickly. Larger applications have dozens of tables, this can be annoying if you want to drop them all, but don’t want to drop the database.

Solution:

Most PHP applications that deal with MySQL will use a table prefix when generating their tables. Using this prefix and calling MySQL from shell we can use this command to drop all the tables with a specific prefix. Just fill in the coloured bits:

mysql -u username database_name -e "show tables" | grep "tableprefix_" | gawk '{print "drop table " $1 ";"}' | mysql -u username database_name

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS