Skip to main content
Drizzle ORM provides a type-safe API for deleting records from your database tables.

Basic Delete

Delete rows with a where clause:
Always include a where() clause unless you intentionally want to delete all rows in the table.

Delete with Multiple Conditions

Combine conditions to delete specific rows:

Delete with OR Conditions

Delete rows matching any of several conditions:

Delete with Returning

Get the deleted row(s) back from the database:
.returning() is supported in PostgreSQL, SQLite, and MySQL 8.0+. It’s not available in older MySQL versions.

Delete by ID

Common pattern for deleting by primary key:

Delete Multiple Rows

Delete rows using array of values:

Delete All Rows

Delete every row in a table (use with extreme caution):
Omitting the where() clause deletes ALL rows in the table. This operation cannot be undone. Always double-check before running such queries.

Delete with Pattern Matching

Delete based on pattern matching:

Delete with Null Checks

Delete based on null values:

Delete with Comparison Operators

Delete using various comparison operators:

Delete with Subqueries

Delete using subquery conditions:

Soft Delete Pattern

Instead of deleting, mark records as deleted:
Soft deletes preserve data and maintain referential integrity while allowing easy recovery.

Delete in Transactions

Delete within a transaction for consistency:

Cascade Deletes

Handle related data when deleting:

Conditional Delete Logic

Build dynamic delete queries:

Delete with Limits (MySQL)

Limit the number of rows deleted:
LIMIT in DELETE is MySQL-specific. PostgreSQL and SQLite don’t support this directly.

Type Safety

Drizzle ensures type safety for deletes:

Common Delete Patterns

1

Archive Before Delete

2

Delete Expired Records

3

Cleanup Orphaned Records

4

Delete Duplicates

Safety Checklist

Always test delete queries in development first
Use transactions when deleting related records
Consider soft deletes for important data
Backup data before running bulk deletes
Use WHERE clauses to avoid accidental mass deletion

Performance Tips

Index Where Columns

Ensure columns in WHERE clauses are indexed for faster deletes

Batch Deletes

Use inArray() to delete multiple rows efficiently

Disable Triggers Temporarily

For bulk deletes, consider temporarily disabling triggers

Vacuum After Large Deletes

Run VACUUM (PostgreSQL) or OPTIMIZE (MySQL) after deleting many rows

Next Steps

Update Queries

Learn about updating data

Select Queries

Query your data

Transactions

Ensure data consistency with transactions