Truncate all tables in a MySQL database in one command?

devang picture devang · Dec 16, 2009 · Viewed 437.3k times · Source

Is there a query (command) to truncate all the tables in a database in one operation? I want to know if I can do this with one single query.

Answer

battousaix picture battousaix · Jan 18, 2012

Drop (i.e. remove tables)

mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "drop table $table" DATABASE_NAME; done

Truncate (i.e. empty tables)

mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "truncate table $table" DATABASE_NAME; done