How to Drop All Tables in MySQL

  1. SQL Query to Drop All Tables
  2. Empty and Recreate the Database
  3. Verification of Deleted Tables
How to Drop All Tables in MySQL

This tutorial demonstrates several ways a user can drop all tables in MySQL and lists sample scripts to make it possible.

The reason why you can’t outright drop all tables in a single line of code is that in a sizeable and well-designed database, there will likely be a lot of foreign key constraints.

SQL Query to Drop All Tables

This approach is possible because we disabled foreign key checking at the start. So whatever order we delete the tables in, it won’t throw any constraint error. Just make sure to re-enable foreign key checking after the script.

Empty and Recreate the Database

For this to be possible, you need to make sure that other than tables, you also are willing to delete stored functions, indices, roles, and other settings apart from tables since you are dropping the whole database.

For this to be executed successfully, the current role must have drop and create privileges in the database.

SQL
 sqlCopyDROP DATABASE database_name;

After dropping, recreate the database.

SQL
 sqlCopyCREATE DATABASE database_name;

This approach is most likely the most volatile method of dropping all tables. This is suitable only for small or experimental databases.

Use mysqldump to Drop Tables in MySQL

mysqldump is a console command attributed from MySQL. Assuming you already have MySQL set up in your path variables, we can use the command to drop tables within your database.

This will work easier if you have knowledge over console commands and have MySQL set up in your path variable.

Verification of Deleted Tables

To verify whether all tables actually were deleted, we can check the database if it still outputs the tables with this script.

SQL
 sqlCopySHOW TABLES FROM database_name;

If there is no output, that means that the query has been executed successfully, and all SQL tables from that specific database have been dropped.

In summary, the reason why you can’t delete tables in bulk directly in SQL is that relational databases rely heavily on relationships and constraints. If you were allowed to delete tables without explicitly disabling foreign key checking, the relationships, constraints, and foreign key configurations will be corrupted.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Rayven Esplanada avatar Rayven Esplanada avatar

Skilled in Python, Java, Spring Boot, AngularJS, and Agile Methodologies. Strong engineering professional with a passion for development and always seeking opportunities for personal and career growth. A Technical Writer writing about comprehensive how-to articles, environment set-ups, and technical walkthroughs. Specializes in writing Python, Java, Spring, and SQL articles.

LinkedIn

Related Article - MySQL Table