Mysql – Truncate all tables in a MySQL database in one command

MySQL

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.

Best Answer

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