How to DROP TABLE in PostgreSQL

Galaxy Glossary

How do I safely drop a table in PostgreSQL?

DROP TABLE permanently removes one or more tables and all their data from a PostgreSQL database.

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 use the DROP TABLE command?

Use DROP TABLE when you need to permanently delete a table, its data, indexes, constraints, and dependent objects. It frees storage and removes obsolete structures.

What is the basic syntax of DROP TABLE?

DROP TABLE [IF EXISTS] table_name [, ...] [CASCADE | RESTRICT];

How to drop a single table?

Run DROP TABLE IF EXISTS "Customers"; to safely delete one table even if you are unsure it exists.

How to drop multiple tables at once?

List them comma-separated: DROP TABLE Orders, OrderItems;

How to avoid errors when dependencies exist?

Add CASCADE: DROP TABLE Products CASCADE; also removes foreign-key references, views, and triggers.

When should I use CASCADE vs RESTRICT?

CASCADE deletes dependent objects automatically—handy during dev or schema refactors. RESTRICT (default) blocks the drop if dependencies exist—safer for prod.

How does DROP TABLE work in an ecommerce schema?

If you archive orders elsewhere, remove the live table: DROP TABLE IF EXISTS Orders CASCADE;. All related OrderItems rows vanish too because of CASCADE.

What are best practices for safe drops?

1) Always back up first. 2) Run in a transaction and verify with ROLLBACK before COMMIT. 3) Use IF EXISTS to avoid errors in automated scripts.

What are common DROP TABLE mistakes?

See below for quick fixes.

Why How to DROP TABLE in PostgreSQL is important

How to DROP TABLE in PostgreSQL Example Usage


-- Suppose you’ve moved historical orders to cold storage
BEGIN;
DROP TABLE IF EXISTS Orders CASCADE;  -- removes Orders plus dependent OrderItems
COMMIT;

How to DROP TABLE in PostgreSQL Syntax


-- Basic
DROP TABLE [IF EXISTS] name [, ...] [CASCADE | RESTRICT];

-- Example options
-- 1. Drop one table only if it exists
DROP TABLE IF EXISTS Customers;

-- 2. Drop multiple tables
DROP TABLE Orders, OrderItems;

-- 3. Force-drop while removing dependencies
DROP TABLE Products CASCADE;

Common Mistakes

Frequently Asked Questions (FAQs)

Does DROP TABLE auto-commit?

No. It runs inside the current transaction. Wrap in BEGINCOMMIT to control rollbacks.

Can I recover a dropped table?

Only from backups or point-in-time recovery. DROP TABLE is irreversible once committed.

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.