SQL Keywords

SQL DROP DATABASE

What is SQL DROP DATABASE?

Permanently removes an entire database and all its objects from the server.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL DROP DATABASE: Supported: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle (CDB or pluggable databases), Amazon Redshift. Not supported: SQLite.

SQL DROP DATABASE Full Explanation

DROP DATABASE is a Data Definition Language (DDL) command that deletes a database, including every table, view, function, and stored procedure it contains. Once executed, the operation is irreversible; the data cannot be recovered unless backups exist. Most engines require that no active connections use the target database. Many dialects offer an IF EXISTS clause to avoid errors when the database is missing. Some systems also support additional options such as FORCE or WITH (ROLLBACK AFTER) to terminate connections or set timeouts. Because DROP DATABASE affects the entire schema, it should be executed only by privileged users in maintenance windows or automated teardown scripts.

SQL DROP DATABASE Syntax

DROP DATABASE [IF EXISTS] database_name;

SQL DROP DATABASE Parameters

  • IF EXISTS (keyword) - Suppresses an error if the database does not exist
  • database_name (identifier) - The name of the database to drop

Example Queries Using SQL DROP DATABASE

-- Basic drop
DROP DATABASE sales_archive;

-- Safe drop if present
DROP DATABASE IF EXISTS temp_testing;

-- Force drop in PostgreSQL 13+
DROP DATABASE inventory_db WITH (FORCE);

Expected Output Using SQL DROP DATABASE

  • The specified database is removed from the server catalog
  • All contained objects are deleted, active connections are terminated (or the statement fails if they remain), and any future reference to the database name will throw an error until it is recreated

Use Cases with SQL DROP DATABASE

  • Decommissioning obsolete environments (staging, test)
  • Cleaning up temporary databases created in CI pipelines
  • Removing corrupted databases after migration to new schema
  • Rebuilding demo or sandbox instances on demand

Common Mistakes with SQL DROP DATABASE

  • Forgetting to disconnect active sessions, leading to errors such as "database is being accessed by other users"
  • Executing the command in the wrong environment or on the wrong server
  • Assuming the action is reversible without backups
  • Omitting IF EXISTS, which causes an error if the database does not exist

Related Topics

CREATE DATABASE, ALTER DATABASE, DROP SCHEMA, DROP TABLE, BACKUP DATABASE

First Introduced In

MySQL 3.22; later adopted by PostgreSQL 7.3 and SQL Server 2000

Frequently Asked Questions

How do I drop a database only if it exists?

Use the IF EXISTS clause: `DROP DATABASE IF EXISTS db_name;` This prevents an error if the database is already gone.

Why does my DROP DATABASE command hang?

Active connections are still using the target database. Terminate sessions or use options like `WITH (FORCE)` in PostgreSQL.

Does DROP DATABASE remove backups?

No. It deletes the live database files but does not delete external backups or snapshots.

Who can run DROP DATABASE?

Only users with superuser or DROP privilege on the database (often DBAs or admin roles) can execute this command safely.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!