MySQL Drop Database
Learn how to delete databases in MySQL
⚠️ What is DROP DATABASE?
The DROP DATABASE statement permanently deletes a database and all its contents including tables, data, and objects. This action cannot be undone, so use it carefully in production environments.
-- Delete a database permanently
DROP DATABASE old_store;
Output:
Query OK, 0 rows affected (0.15 sec)
⚠️ Database and all data deleted permanently
Key Concepts
Permanent Deletion
Cannot be undone or recovered
DROP DATABASE testdb;
IF EXISTS
Prevents errors if database missing
DROP DATABASE IF EXISTS testdb;
All Data Lost
Tables and data are deleted
-- Everything inside is deleted
DROP DATABASE shop;
Immediate Action
Executes instantly without confirmation
-- No "Are you sure?" prompt
DROP DATABASE olddb;
🔹 Basic DROP DATABASE Syntax
The DROP DATABASE statement removes a database completely from the MySQL server. Once executed, all tables, views, stored procedures, and data within the database are permanently deleted and cannot be recovered without a backup.
-- Basic syntax
DROP DATABASE database_name;
-- Example: Delete a test database
DROP DATABASE test_db;
-- Example: Delete an old project database
DROP DATABASE old_project;
Output:
Database 'test_db' dropped successfully
Database 'old_project' dropped successfully
⚠️ All data permanently deleted
🔹 DROP DATABASE IF EXISTS
Using IF EXISTS prevents errors when attempting to drop a database that doesn't exist. This is particularly useful in automated scripts, migrations, or cleanup operations where you want the script to continue running even if the database is already gone.
-- Safe database deletion
DROP DATABASE IF EXISTS temp_db;
-- This won't cause an error even if database doesn't exist
DROP DATABASE IF EXISTS non_existent_db;
-- Example in a cleanup script
DROP DATABASE IF EXISTS old_backup;
DROP DATABASE IF EXISTS staging_test;
Output:
Query OK, 0 rows affected (0.05 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Note: 1 warning (database doesn't exist)
🔹 Checking Before Dropping
Before dropping a database, it's good practice to verify which databases exist and check if the target database is currently in use. This helps prevent accidental deletion of important data and ensures you're dropping the correct database.
-- Step 1: List all databases
SHOW DATABASES;
-- Step 2: Check current database
SELECT DATABASE();
-- Step 3: View database size before dropping
SELECT
table_schema AS 'Database',
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)'
FROM information_schema.tables
WHERE table_schema = 'mydb'
GROUP BY table_schema;
Output:
| Database | Size (MB) |
|---|---|
| mydb | 45.23 |
🔹 Alternative: DROP SCHEMA
DROP SCHEMA is a synonym for DROP DATABASE in MySQL. Both commands perform exactly the same operation and can be used interchangeably. Some developers prefer DROP SCHEMA for consistency with SQL standards used in other database systems.
-- DROP SCHEMA works the same as DROP DATABASE
DROP SCHEMA test_schema;
-- With IF EXISTS clause
DROP SCHEMA IF EXISTS old_schema;
-- Both commands are identical
DROP DATABASE mydb;
DROP SCHEMA mydb; -- Same result
Output:
Schema 'test_schema' dropped successfully
✓ DROP SCHEMA and DROP DATABASE are equivalent
🔹 Complete Example with Safety Checks
This example demonstrates a safe approach to dropping a database with proper verification steps. Always backup important data before dropping databases, and verify you're not currently using the database you intend to delete to avoid connection errors.
-- Step 1: Show all databases
SHOW DATABASES;
-- Step 2: Make sure you're not using the database
SELECT DATABASE();
-- Step 3: Switch to a different database if needed
USE mysql;
-- Step 4: Safely drop the database
DROP DATABASE IF EXISTS old_project;
-- Step 5: Verify deletion
SHOW DATABASES LIKE 'old_project';
Output:
✓ Switched to 'mysql' database
✓ Database 'old_project' dropped
Empty set (0.00 sec) - Database no longer exists
⚠️ Important Warnings
- No Undo: DROP DATABASE is permanent and irreversible
- Backup First: Always backup important data before dropping
- Check Twice: Verify database name before executing
- Production Caution: Never drop production databases without approval
- Permissions: Requires DROP privilege on the database
- Active Connections: May fail if users are connected to the database
💡 Best Practices
- Always use IF EXISTS in automated scripts
- Create backups before dropping databases
- Document why a database is being dropped
- Use descriptive names for temporary databases (e.g., temp_*, test_*)
- Verify you're not currently using the database before dropping