24

I don't want to drop database,

because I'm not hosting the website on my own machine,

drop the database will require create it again,and many settings.

Is there a command in MySQL that can be used to delete all tables in a specific database?

EDIT

Everything I can do is within a phpMyAdmin

6 Answers 6

46

You were talking about doing this in phpMyAdmin.

I just had to do this and I'm not sure what version you are using but in the version I have if you scroll to the bottom of the table list you can click "Check All" and then in the drop down next to it that has "With Selected:" you can select "Drop" and it empties all the tables.

2
  • 8
    This is the actual answer for this question.
    – Md Faisal
    Feb 25, 2016 at 14:23
  • It works! But i disabled verify foreign key to drop all tables! Dec 20, 2016 at 15:06
8

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]

Here there are more methods to drop all tables without dropping the database.

2
  • How do I use your script with when I get this error: Cannot delete or update a parent row: a foreign key constraint fails Jul 27, 2013 at 13:31
  • @user2298943 Use this: mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | grep -e '^DROP \| FOREIGN_KEY_CHECKS' | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]
    – kiLLua
    Feb 3, 2017 at 1:51
8

You can also generate a sql file from the information_schema database:

Select concat('DROP TABLE database_name.', table_name,';') from information_schema.TABLES where table_schema='database_name';

This will give an output like:

DROP TABLE database_name.table1;
DROP TABLE database_name.table2;
[...]
DROP TABLE database_name.tableN;
3

I'm not aware of anything direct to suit your needs. You might try doing the following:

Run this to show your drop commands:

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | 
grep ^DROP


Nex you can do this to actually execute the drop commands:

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | 
grep ^DROP | 
mysql -u[USERNAME] -p[PASSWORD] [DATABASE]
0
$q=mysql_query("SHOW TABLES FROM ".SQL_DATABASE_NAME);

while($r=mysql_fetch_assoc($q)){
    mysql_query("DROP TABLE ".$r["Tables_in_".SQL_DATABASE_NAME]); 
}

it work for me!

0

If your table names have only simple names (no spaces or other special chars) this query could build the whole drop command for you:

select concat('drop table ', group_concat(table_name), ';') from information_schema.tables where table_schema='yourdbname';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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