Basic Syntax
FROM keyword is optional.
Delete Single Record
| Database | Output |
|---|---|
| PostgreSQL | DELETE FROM users WHERE id = 1 |
| MySQL | DELETE FROM users WHERE id = 1 |
| MongoDB | db.users.deleteOne({ _id: 1 }) |
| Redis | DEL users:1 |
Delete with Conditions
| Database | Output |
|---|---|
| PostgreSQL | DELETE FROM users WHERE status = 'inactive' AND last_login < '2023-01-01' |
| MongoDB | db.users.deleteMany({ status: 'inactive', last_login: { $lt: '2023-01-01' } }) |
Delete with IN
| Database | Output |
|---|---|
| PostgreSQL | DELETE FROM users WHERE id IN (1, 2, 3, 4, 5) |
| MongoDB | db.users.deleteMany({ _id: { $in: [1, 2, 3, 4, 5] } }) |
Delete with LIKE
| Database | Output |
|---|---|
| PostgreSQL | DELETE FROM logs WHERE message LIKE '%debug%' |
| MongoDB | db.logs.deleteMany({ message: { $regex: 'debug' } }) |
Delete with NULL Check
| Database | Output |
|---|---|
| PostgreSQL | DELETE FROM users WHERE email IS NULL |
| MongoDB | db.users.deleteMany({ email: null }) |
Delete with BETWEEN
| Database | Output |
|---|---|
| PostgreSQL | DELETE FROM logs WHERE created_at BETWEEN '2023-01-01' AND '2023-06-30' |
Complete Examples
Remove Expired Sessions
Clean Up Old Logs
Remove Unverified Users
Cancel Abandoned Orders
Remove Test Data
Soft Delete Alternative
Instead of deleting, consider soft delete:Truncate
Remove all records from a table. Both syntaxes work:| Database | Output |
|---|---|
| PostgreSQL | TRUNCATE TABLE logs |
| MySQL | TRUNCATE TABLE logs |
| MongoDB | db.logs.deleteMany({}) |

