How to drop all MySQL tables from the command-line?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay
--
Chapters
00:00 How To Drop All Mysql Tables From The Command-Line?
00:25 Accepted Answer Score 32
00:44 Answer 2 Score 13
01:15 Answer 3 Score 2
01:35 Thank you
--
Full question
https://superuser.com/questions/308071/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#commandline #terminal #mysql
#avk47
ACCEPTED ANSWER
Score 32
You can drop the database then immediately recreate it:
mysql> drop database [database name];
mysql> create database [database name];
Or you could use a script to drop each table in the database.
ANSWER 2
Score 13
You can try the following command:
mysqldump --no-data --add-drop-table DB_NAME | grep ^DROP | mysql -v DB_NAME
Or:
mysql --silent --skip-column-names -e "SHOW TABLES" DB_NAME | xargs -L1 -I% echo 'DROP TABLE `%`;' | mysql -v DB_NAME
Where DB_NAME
is your database name. Database credentials you can specify either in ~/.my.cnf
or adding them to the command (e.g. -uroot -proot
).
This method has some advantages over dropping and creating the database in case your database user doesn't have permission to drop it.
ANSWER 3
Score 2
mysql -u USERHERE -pPASSWORDHERE --silent --skip-column-names -e "SHOW TABLES" DATABASENAMEHERE | xargs -L1 -I% echo 'SET FOREIGN_KEY_CHECKS = 0; DROP TABLE
%; SET FOREIGN_KEY_CHECKS = 1;' | mysql -u USERHERE -pPASSWORDHERE -v DATABASENAMEHERE