Skip to main content
Remove records using the DELETE operation.

Basic Syntax

:DELETE Entity WHERE condition
:DELETE FROM Entity WHERE condition
Both forms are valid. The FROM keyword is optional.

Delete Single Record

:DELETE User WHERE id = 1
DatabaseOutput
PostgreSQLDELETE FROM users WHERE id = 1
MySQLDELETE FROM users WHERE id = 1
MongoDBdb.users.deleteOne({ _id: 1 })
RedisDEL users:1

Delete with Conditions

:DELETE User WHERE status = "inactive" AND last_login < "2023-01-01"
DatabaseOutput
PostgreSQLDELETE FROM users WHERE status = 'inactive' AND last_login < '2023-01-01'
MongoDBdb.users.deleteMany({ status: 'inactive', last_login: { $lt: '2023-01-01' } })

Delete with IN

:DELETE User WHERE id IN (1, 2, 3, 4, 5)
DatabaseOutput
PostgreSQLDELETE FROM users WHERE id IN (1, 2, 3, 4, 5)
MongoDBdb.users.deleteMany({ _id: { $in: [1, 2, 3, 4, 5] } })

Delete with LIKE

:DELETE Log WHERE message LIKE "%debug%"
DatabaseOutput
PostgreSQLDELETE FROM logs WHERE message LIKE '%debug%'
MongoDBdb.logs.deleteMany({ message: { $regex: 'debug' } })

Delete with NULL Check

:DELETE User WHERE email IS NULL
DatabaseOutput
PostgreSQLDELETE FROM users WHERE email IS NULL
MongoDBdb.users.deleteMany({ email: null })

Delete with BETWEEN

:DELETE Log WHERE created_at BETWEEN "2023-01-01" AND "2023-06-30"
DatabaseOutput
PostgreSQLDELETE FROM logs WHERE created_at BETWEEN '2023-01-01' AND '2023-06-30'

Complete Examples

Remove Expired Sessions

:DELETE Session WHERE expires_at < "2024-01-15T00:00:00Z"

Clean Up Old Logs

:DELETE Log WHERE created_at < "2023-01-01" AND level = "debug"

Remove Unverified Users

:DELETE User WHERE verified = false AND created_at < "2023-12-01"

Cancel Abandoned Orders

:DELETE Order WHERE status = "pending" AND created_at < "2024-01-01"

Remove Test Data

:DELETE User WHERE email LIKE "%@test.com"

Soft Delete Alternative

Instead of deleting, consider soft delete:
-- Soft delete (recommended)
:UPDATE User SET deleted_at:"2024-01-15T10:30:00Z", active:false WHERE id = 1

-- Hard delete (permanent)
:DELETE User WHERE id = 1

Truncate

Remove all records from a table. Both syntaxes work:
:TRUNCATE Log
:TRUNCATE TABLE Log
DatabaseOutput
PostgreSQLTRUNCATE TABLE logs
MySQLTRUNCATE TABLE logs
MongoDBdb.logs.deleteMany({})
Warning: TRUNCATE removes ALL records and cannot be rolled back in most databases.

Warning

Always include a WHERE clause to avoid deleting all records accidentally.
-- DANGEROUS: Deletes ALL users
:DELETE User

-- SAFE: Deletes specific user
:DELETE User WHERE id = 1

Next Steps