MySQL Truncating or Dropping All Tables in a Database

I just had the need to truncate all the tables in of my databases. Basically I wanted the data gone but the schema to stay. Below is the linux shell command I used to truncate the tables.

I’ve also included how to straight up drop the tables too- just keep in mind dropping tables dumps the schema and data.

Truncate

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

Drop

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

Make sure the user executing this has privileges to the database in question.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s