Usually I open Terminal.app and connect to a remote MySQL database.

Then I use this command to drop a table:

mysql> drop table [table name];

But what I need is the command line to drop all tables in the database.

If I use:

mysql> drop database [database name];

I'll destroy the database completely and I won't be able to create tables again. Am I right?


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.

  • ok, just to be sure: I connect to remote database in Terminal with this line: /Applications/MAMP/Library/bin/mysql -h 80.54.554.10 -u adm_user -p my_db. After dropping and creating the database as you said will I need to change this connection values or they remain the same? – chefnelone Jul 8 '11 at 11:57
  • 1
    @chef - Dropping all tables and dropping the whole database does not have the same effect. If you drop only the tables any database wide permissions will remain, not so if you drop/recreate the database. – Nifle Jul 8 '11 at 21:16
  • 10
    @chefnelone : Be careful !!! Dropping a database drops stored procedures and user-defined views as well. – RolandoMySQLDBA Jul 9 '11 at 1:03
  • Quite clear for me. I'll go with the script to delete each table. – chefnelone Jul 11 '11 at 7:31
  • 1
    it's totally unsafe, see the comments http://stackoverflow.com/a/12403742/1713660 – vladkras Oct 29 '15 at 8:01

You can try the following command:

mysqldump --no-data --add-drop-table DB_NAME | grep ^DROP | mysql -v DB_NAME


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.

  • 1
    Confirmed second example, awesome. Add db credentials as additional flags before --silent and -v / --verbose respectively like: --user=username --password=password --host=host.name – here May 20 '15 at 3:41
  • Good idea. But have some problem when there is FOREIGN KEY. I add | sort -r to reverse line, but still not compatible some db. – Fancyoung Dec 3 '15 at 6:45
  • Thanks for the second example. I also needed to deactivate foreign key check before dropping tables: mysql --silent --skip-column-names -e "SHOW TABLES" DB_NAME | xargs -L1 -I% echo 'DROP TABLE `%`;' | mysql --init-command="SET SESSION FOREIGN_KEY_CHECKS=0;" -v DB_NAME – Nico Mar 12 at 15:46


  • 2
    Welcome to Super User!  While this may answer the question, it would be a better answer if you could explain why it does so.  We’re looking for comprehensive, high-quality answers that provide some explanation and context.  Don’t just give a one-line answer; explain why your answer is right, ideally with citations.  Answers that don’t include explanations may be removed.  Also, check your formatting. – G-Man Says 'Reinstate Monica' May 29 '15 at 1:20
  • Not forget set foreign_key_checks = 1 after drop tables: see stackoverflow.com/a/2873991/4306855 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 – toto21 Nov 14 '15 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.