iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

SQL DROP DATABASE

DROP DATABASE removes a database and every table, view, function, and row inside it. No undo.

Basic form

SQL
DROP DATABASE shop;

If exists

SQL
DROP DATABASE IF EXISTS shop_test;

Common in test setup scripts — drop, recreate, then load fixtures.

What happens to active connections?

DBBehaviour
MySQLDrops succeeds; queries on dropped DB return an error.
PostgreSQLFails if anyone is connected. Disconnect first or use WITH (FORCE) on PG 13+.
SQL ServerFails if anyone is connected. ALTER DATABASE … SET SINGLE_USER first.

Safer pattern in production

  1. Take a backup (mysqldump, pg_dump, native backup).
  2. Rename instead of drop: ALTER DATABASE shop RENAME TO shop_retired_2026; (Postgres).
  3. Wait a week. Then drop.
Tip: Treat DROP DATABASE like rm -rf / — even with a backup, the recovery is painful. Run it manually, against the right environment, with someone reviewing your terminal.

Example

Example
DROP DATABASE shop;
Try it Yourself »

Exercise

Drop a database safely only when it exists.

DROP DATABASE EXISTS shop_test;

Test yourself

Q1. DROP DATABASE removes…
Q2. A safer pattern before dropping is…
Q3. In Postgres, DROP DATABASE fails if…

Discussion

Loading…