← Back to PostgreSQL Course | Chapter 4: Inserting & Modifying Data | Lesson 4 of 7

DELETE

DELETE removes rows that match a condition.

In this page:

  1. DELETE
Syntax
sql
DELETE FROM table_name
WHERE condition;

DELETE

DELETE FROM table WHERE condition removes matching rows, while DELETE FROM table without WHERE removes every row but keeps the table structure. Deleted rows are marked dead and cleaned up later by VACUUM. Foreign keys can restrict or cascade deletes.

Note: Try the WHERE condition with SELECT before deleting.

Example: DELETE

sql
CREATE TABLE sessions (id INTEGER PRIMARY KEY, user_name TEXT, expired INTEGER);
INSERT INTO sessions VALUES (1, 'ada', 1), (2, 'bob', 0), (3, 'cy', 1), (4, 'di', 0);
SELECT COUNT(*) AS before_delete FROM sessions;
DELETE FROM sessions WHERE expired = 1;
SELECT user_name FROM sessions ORDER BY id;

-- Output:
-- before_delete
-- 4
-- user_name
-- bob
-- di
Related Topics
Common Mistakes
  1. Forgetting the WHERE clause
  2. Deleting rows referenced by foreign keys
  3. Expecting disk space to be freed instantly
Chapter Summary
  • DELETE FROM removes rows
  • WHERE picks the rows
  • Without WHERE it empties the table
  • VACUUM reclaims space later
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.