How do you delete records in PostgreSQL?

Answer

Use the DELETE FROM statement. DELETE FROM tablename WHERE condition;. Always include WHERE — DELETE FROM tablename; deletes all rows (use TRUNCATE for that instead, which is faster). Delete with RETURNING: DELETE FROM sessions WHERE expires_at < NOW() RETURNING session_id;. Delete using a join (DELETE...USING): DELETE FROM orders USING users WHERE orders.user_id = users.id AND users.is_deleted = true;. TRUNCATE: TRUNCATE tablename; — removes all rows much faster than DELETE (doesn't scan rows), resets sequences if RESTART IDENTITY is specified. TRUNCATE is not reversible without a transaction.