DELETE
DELETE removes rows that match a condition.
In this page:
Syntax
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
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
Login to try C/C++/Java/PHP code in the editor
Related Topics
Common Mistakes
- Forgetting the WHERE clause
- Deleting rows referenced by foreign keys
- 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: