How to Drop All Tables in MySQL

Galaxy Glossary

How do I drop all tables in a MySQL database without deleting the database itself?

Removes every table in the current MySQL database in a single operation.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why drop all tables instead of the whole database?

Dropping only tables keeps the database object, users, and privileges intact while clearing data structures. This is handy in staging environments or when schemas must be rebuilt quickly.

What are the risks of dropping every table?

The action is irreversible; foreign keys and views disappear, and applications that reference them break. Always back up with mysqldump --all-databases or snapshot storage first.

How do you list every table in the current database?

Run SHOW TABLES; or query information_schema.tables. Confirm the count matches expectations before deletion.

How to generate DROP statements dynamically?

Method 1: CONCAT with GROUP_CONCAT

Use GROUP_CONCAT to build one long statement that lists each table, then execute it with PREPARE.

Method 2: information_schema & prepared statement

Cursor through information_schema.tables, concatenate each table name, and call EXECUTE.

What is the safest workflow to drop all tables?

1) Backup. 2) Disable foreign key checks. 3) Generate dynamic SQL. 4) Execute. 5) Re-enable checks. 6) Verify with SHOW TABLES;.

How to rebuild ecommerce schema after a full drop?

Run your migration tool or re-execute CREATE scripts for Customers, Orders, Products, and OrderItems. Validate with sample inserts.

Best practices for mass table deletion

Always wrap in a transaction when available, log executed SQL, and restrict to non-production environments via separate credentials.

Common mistakes and how to avoid them

See below.

FAQ

Is there one command to drop all tables?

No native single command exists; you must build a dynamic DROP statement.

Will foreign keys block the drop?

Yes. Temporarily disable with SET FOREIGN_KEY_CHECKS=0;.

Can I undo after commit?

Only from backups or point-in-time recovery; DROP is permanent.

Why How to Drop All Tables in MySQL is important

How to Drop All Tables in MySQL Example Usage


-- Example: clear all ecommerce tables before re-seeding test data
SET FOREIGN_KEY_CHECKS = 0;
SELECT CONCAT('DROP TABLE IF EXISTS ', GROUP_CONCAT(CONCAT('`', table_name, '`') SEPARATOR ', '), ';')
INTO   @drop_all
FROM   information_schema.tables
WHERE  table_schema = DATABASE();
PREPARE zap FROM @drop_all; EXECUTE zap; DEALLOCATE PREPARE zap;
SET FOREIGN_KEY_CHECKS = 1;
-- Now recreate
SOURCE /migrations/001_create_customers_orders_products.sql;

How to Drop All Tables in MySQL Syntax


-- Disable FK checks
SET FOREIGN_KEY_CHECKS = 0;

-- Generate a single DROP statement for all tables in the current DB
SELECT CONCAT('DROP TABLE IF EXISTS ', GROUP_CONCAT(CONCAT('`', table_name, '`') SEPARATOR ', '), ';') 
INTO   @drop_stmt
FROM   information_schema.tables
WHERE  table_schema = DATABASE();

-- Prepare and execute
PREPARE stmt FROM @drop_stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Re-enable checks
SET FOREIGN_KEY_CHECKS = 1;

Common Mistakes

Frequently Asked Questions (FAQs)

Do I need super privileges to drop all tables?

You need DROP privileges on each table or DROP privilege for the database.

How can I exclude certain tables?

Add a NOT IN ('table1','table2') filter in the WHERE table_name clause when building the dynamic SQL.

Is TRUNCATE faster than DROP?

TRUNCATE keeps the table structures. DROP removes them. TRUNCATE is slightly faster but unsuitable when you need to rebuild schemas.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.