Warming up the neural circuits...
Sometimes you need to remove a table entirely or clear all its data. By the end of this module you will:
DROP TABLETRUNCATE TABLEWhat it is: DROP TABLE is a DDL command that completely removes a table from the database — the structure, all data, indexes, constraints, and triggers are permanently deleted.
Why we use it: To remove tables that are no longer needed — deprecated features, test tables, or temporary data structures. It's also used in development to recreate tables from scratch.
When we use it: During cleanup of test data, when removing deprecated features, or in development when you need to reset a table's structure.
-- Remove a table
DROP TABLE products;
-- Safe removal (prevents error if table doesn't exist)
DROP TABLE IF EXISTS products;This is irreversible. The table, its data, its indexes, and its constraints are all gone. Always verify with \dt before dropping.
If other tables reference this table via foreign keys, DROP TABLE will fail. Use CASCADE to remove dependencies too.
-- Fails if other tables reference this one
DROP TABLE users;
-- Removes the table AND all foreign key references
DROP TABLE users CASCADE;What it is: TRUNCATE TABLE is a DDL command that instantly removes all rows from a table without scanning each row individually. Unlike DELETE, it doesn't check WHERE conditions or fire row-level triggers — it just empties the table.
Why we use it: When you need to clear a table quickly (test data, temporary data, logs), TRUNCATE is orders of magnitude faster than DELETE because it doesn't need to scan and delete rows one by one.
When we use it: During test cleanup, when resetting tables for new data imports, or when clearing temporary/staging tables.
-- Clear all data from a table
TRUNCATE TABLE products;
-- Clear multiple tables at once
TRUNCATE TABLE products, orders, users;| Feature | TRUNCATE | DELETE |
|---|---|---|
| Speed | Very fast (doesn't scan rows) | Slower (scans and deletes one by one) |
| WHERE clause | Not supported | Supported |
| Resets sequences | Yes (with RESTART IDENTITY) | No |
| Triggers | Doesn't fire row-level triggers | Fires triggers |
| Rollback | Can be rolled back in a | Can be rolled back |
-- Truncate and reset the auto-increment counter
TRUNCATE TABLE products RESTART IDENTITY;
-- Truncate and cascade to referenced tables
TRUNCATE TABLE products CASCADE;What it is: These three commands all remove data, but they differ in scope, speed, and reversibility. Understanding when to use each is critical for safe database management.
Why we use it: Choosing the wrong command can lead to data loss (DROP when you meant TRUNCATE) or performance issues (DELETE when TRUNCATE would be instant). This comparison helps you pick the right tool.
When we use it: Every time you need to remove data — the choice depends on whether you want to keep the structure, how much data to remove, and whether you need WHERE filtering.
| Command | What it does | Reversible? | Keeps structure? |
|---|---|---|---|
DROP TABLE | Removes table + data | No | No |
TRUNCATE TABLE | Removes all rows | In a transaction | Yes |
DELETE FROM | Removes rows (with WHERE) | In a transaction | Yes |
Rendering diagram…
\dt first to verify the table nameRESTART IDENTITYtemp_data with columns id SERIAL PRIMARY KEY and value TEXTtemp_dataSELECT * FROM temp_dataTRUNCATE TABLE temp_data RESTART IDENTITY to clear itSELECT * FROM temp_dataDROP TABLE IF EXISTS temp_data\dtNext up: Now that you can create and manage tables, it's time to put data into them. In the next module, you'll master INSERT — adding single rows, multiple rows, and using RETURNING to get immediate feedback.