DELETE

Updated · 8 min read
easyPostgreSQL 18sqldeletetruncatesoft-deletechanging-datasafety

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 reports DELETE n.
  • Without WHERE, every row is deleted. Run a SELECT with the same WHERE first.
  • A foreign key refuses to delete a row that other rows still point to.
  • TRUNCATE empties a whole table quickly; it has no WHERE.
  • A soft delete marks a row as deleted (for example with a deleted_on date) instead of removing it, so history is kept.

The Basics

SQL
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:

SQL▶ Try it
DELETE FROM payments
WHERE payment_id = 7
RETURNING payment_id, student_id, amount_usd;
Output
 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:

SQL▶ Try it
DELETE FROM payments
WHERE payment_id = 7;
 
SELECT count(*) AS payments_left
FROM payments;
Output
DELETE 1
 
 payments_left
---------------
             6
(1 row)

DELETE 1, and six payments remain.

WHERE picks payment 7; only that row is removed

Delete Safely

The same habits as UPDATE apply, and they matter even more, because a deleted row is gone.

  1. Run a SELECT with the same WHERE, and check exactly which rows it returns.
  2. Start a transaction with BEGIN, run the DELETE, and check the DELETE n count.
  3. COMMIT if it's right, or ROLLBACK if it isn't.

Here a payment-cleanup goes wrong and is undone:

SQL▶ Try it
BEGIN;
 
DELETE FROM payments
WHERE paid_on < '2025-12-01';
 
ROLLBACK;
 
SELECT count(*) AS payments
FROM payments;
Output
BEGIN
 
DELETE 3
 
ROLLBACK
 
 payments
----------
        7
(1 row)

The DELETE removed three payments, and ROLLBACK put them back: all seven are still there.

You can't delete a row that other rows still point to. Navika has an enrollment, so deleting her student row is refused:

SQL▶ Try it
DELETE FROM students
WHERE student_id = 103;
Output
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:

SQL▶ Try it
DELETE FROM courses
WHERE course_id = 1;
Output
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:

SQL▶ Try it
DELETE FROM courses
WHERE course_id = 6
RETURNING course_id, title;
Output
 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.)

Navika's row is protected by her enrollment; Git Basics has nothing pointing to it

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:

SQL▶ Try it
TRUNCATE payments;
 
SELECT count(*) AS payments
FROM payments;
Output
TRUNCATE TABLE
 
 payments
----------
        0
(1 row)
DELETETRUNCATE
RemovesThe rows WHERE picks (all if no WHERE)Every row
Speed on a big tableRow by row, slowerVery fast
ReportsDELETE nTRUNCATE TABLE
Generated ids afterwardsContinue (the next payment is 8)Continue, unless you add RESTART IDENTITY
A table other tables point toEach row is checkedRefused, unless the referencing tables are truncated too
Inside a transaction in PostgreSQLCan be rolled backCan be rolled back

TRUNCATE refuses a table that a foreign key points to, rather than checking each row:

SQL▶ Try it
TRUNCATE students;
Output
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:

SQL▶ Try 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;
Output
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.

DELETE removes the row; a soft delete marks it and keeps it

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
SQL▶ Try it
DELETE FROM enrollments
WHERE student_id = 102 AND course_id = 1
RETURNING *;
 
SELECT count(*) AS advitha_enrollments
FROM enrollments
WHERE student_id = 102;
Output
 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.

Quick check: DELETE

3 questions
1What does DELETE FROM payments; do?
2Why is DELETE FROM students WHERE student_id = 103; refused?
3Which statement removes every row fastest on a large table?

Key Takeaways

You want toWrite
Delete one rowDELETE FROM payments WHERE payment_id = 7;
See what was deleted... RETURNING *;
Empty a tableTRUNCATE payments;
Be able to undoBEGIN; DELETE ...; ROLLBACK;
Soft deleteUPDATE payments SET deleted_on = '2026-01-05' WHERE payment_id = 7;

Was this tutorial helpful?

Lesson 23 of 115