DELETE
DELETE removes rows from a table. A test payment entered by mistake, a course the program stops offering: each disappears with a DELETE. Like UPDATE, it's short and powerful, and WHERE decides which rows go; without WHERE, every row goes. This lesson covers deleting safely, how foreign keys stop you from deleting a row other rows depend on, the difference between DELETE and TRUNCATE, and the soft delete many real systems use instead. Every Try it run starts from a fresh copy of the Fee Tracker, so delete freely here.
In One Minute
DELETE FROM table WHERE condition;removes the matching rows; PostgreSQL reportsDELETE n.- Without
WHERE, every row is deleted. Run aSELECTwith the sameWHEREfirst. - A foreign key refuses to delete a row that other rows still point to.
TRUNCATEempties a whole table quickly; it has noWHERE.- A soft delete marks a row as deleted (for example with a
deleted_ondate) instead of removing it, so history is kept.
The Basics
DELETE FROM table_name
WHERE condition;Payment 7 was entered twice by mistake, so the copy is removed. RETURNING (PostgreSQL only) shows what was deleted:
DELETE FROM payments
WHERE payment_id = 7
RETURNING payment_id, student_id, amount_usd; payment_id | student_id | amount_usd
------------+------------+------------
7 | 103 | 225.00
(1 row)Without RETURNING, PostgreSQL reports the count, which is worth checking against what you expected:
DELETE FROM payments
WHERE payment_id = 7;
SELECT count(*) AS payments_left
FROM payments;DELETE 1
payments_left
---------------
6
(1 row)DELETE 1, and six payments remain.
Delete Safely
The same habits as UPDATE apply, and they matter even more, because a deleted row is gone.
- Run a
SELECTwith the sameWHERE, and check exactly which rows it returns. - Start a transaction with
BEGIN, run theDELETE, and check theDELETE ncount. COMMITif it's right, orROLLBACKif it isn't.
Here a payment-cleanup goes wrong and is undone:
BEGIN;
DELETE FROM payments
WHERE paid_on < '2025-12-01';
ROLLBACK;
SELECT count(*) AS payments
FROM payments;BEGIN
DELETE 3
ROLLBACK
payments
----------
7
(1 row)The DELETE removed three payments, and ROLLBACK put them back: all seven are still there.
Foreign Keys Protect Related Rows
You can't delete a row that other rows still point to. Navika has an enrollment, so deleting her student row is refused:
DELETE FROM students
WHERE student_id = 103;ERROR: update or delete on table "students" violates foreign key constraint "enrollments_student_id_fkey" on table "enrollments"
DETAIL: Key (student_id)=(103) is still referenced from table "enrollments".The foreign key on enrollments.student_id protects the data: deleting Navika would leave an enrollment for a student who doesn't exist. Courses are protected the same way, even by other courses: Java Basics is the prerequisite of Java OOP:
DELETE FROM courses
WHERE course_id = 1;ERROR: update or delete on table "courses" violates foreign key constraint "courses_prerequisite_id_fkey" on table "courses"
DETAIL: Key (course_id)=(1) is still referenced from table "courses".A row nothing points to can be deleted. Git Basics has no enrollments, no payments and isn't anyone's prerequisite:
DELETE FROM courses
WHERE course_id = 6
RETURNING course_id, title; course_id | title
-----------+------------
6 | Git Basics
(1 row)To delete a row that others depend on, delete or change the dependent rows first. (A foreign key can also be set up to delete dependent rows automatically, with ON DELETE CASCADE; FOREIGN KEY explains when that's wise.)
DELETE Versus TRUNCATE
TRUNCATE empties a whole table in one step. It has no WHERE, and it's much faster than DELETE on a big table, because it doesn't remove rows one by one:
TRUNCATE payments;
SELECT count(*) AS payments
FROM payments;TRUNCATE TABLE
payments
----------
0
(1 row)DELETE | TRUNCATE | |
|---|---|---|
| Removes | The rows WHERE picks (all if no WHERE) | Every row |
| Speed on a big table | Row by row, slower | Very fast |
| Reports | DELETE n | TRUNCATE TABLE |
| Generated ids afterwards | Continue (the next payment is 8) | Continue, unless you add RESTART IDENTITY |
| A table other tables point to | Each row is checked | Refused, unless the referencing tables are truncated too |
| Inside a transaction in PostgreSQL | Can be rolled back | Can be rolled back |
TRUNCATE refuses a table that a foreign key points to, rather than checking each row:
TRUNCATE students;ERROR: cannot truncate a table referenced in a foreign key constraint
DETAIL: Table "enrollments" references "students".
HINT: Truncate table "enrollments" at the same time, or use TRUNCATE ... CASCADE.PostgreSQL can roll back a TRUNCATE inside a transaction. That isn't true everywhere: MySQL and Oracle commit a TRUNCATE immediately, so it can't be undone there.
Soft Delete: Keeping the History
Many real systems never delete important rows. Instead, they add a column such as deleted_on and fill it in, so the row is hidden from normal queries but still available for audits, refunds and reports. Here a deleted_on column is added to payments for this run (you'll learn ALTER TABLE later), payment 7 is marked as deleted, and the normal query hides it:
ALTER TABLE payments ADD COLUMN deleted_on DATE;
UPDATE payments
SET deleted_on = '2026-01-05'
WHERE payment_id = 7;
SELECT payment_id, amount_usd, deleted_on
FROM payments
WHERE deleted_on IS NULL
ORDER BY payment_id;ALTER TABLE
UPDATE 1
payment_id | amount_usd | deleted_on
------------+------------+------------
1 | 450.00 | NULL
2 | 250.00 | NULL
3 | 250.00 | NULL
4 | 550.00 | NULL
5 | 200.00 | NULL
6 | 225.00 | NULL
(6 rows)Payment 7 is still in the table with its date of deletion, but every everyday query adds WHERE deleted_on IS NULL. The trade-off: the table keeps growing, and every query must remember the filter. Use a soft delete when history matters, such as money records, and a real DELETE for true mistakes and test data.
Common Mistakes
Forgetting WHERE. Every row goes. Write the WHERE first, check it with a SELECT, and use a transaction.
Deleting the parent before the children. The foreign key refuses it. Delete or update the dependent rows first.
Using TRUNCATE when you meant a few rows. It has no WHERE; it empties the table.
Expecting deleted ids to be reused. After deleting payment 7, the next new payment is still 8. Generated ids keep counting up.
Practice
Challenge: Delete Advitha's enrollment in Java Basics (student 102, course 1), choosing the row by its full key, and use RETURNING * to show what was removed. Then count Advitha's remaining enrollments.
Show solution
DELETE FROM enrollments
WHERE student_id = 102 AND course_id = 1
RETURNING *;
SELECT count(*) AS advitha_enrollments
FROM enrollments
WHERE student_id = 102; student_id | course_id | enrolled_on | status
------------+-----------+-------------+--------
102 | 1 | 2025-12-29 | active
(1 row)
advitha_enrollments
---------------------
1
(1 row)The delete works because Advitha has no payments for Java Basics, so no payment points to that enrollment. One enrollment, SQL Basics, remains.
Key Takeaways
| You want to | Write |
|---|---|
| Delete one row | DELETE FROM payments WHERE payment_id = 7; |
| See what was deleted | ... RETURNING *; |
| Empty a table | TRUNCATE payments; |
| Be able to undo | BEGIN; DELETE ...; ROLLBACK; |
| Soft delete | UPDATE payments SET deleted_on = '2026-01-05' WHERE payment_id = 7; |
Was this tutorial helpful?