Queries
deleteMany
Delete all records matching a filter — with cascade behavior and count return.
Deletes all records matching the where clause. Returns the count of deleted records.
const count = await client.db.User.deleteMany({
where: { isActive: false },
});
// count: numberOptions
| Option | Type | Required | Description |
|---|---|---|---|
where | UserWhereInput | Yes | Filter conditions to match records for deletion |
Basic Usage
const count = await client.db.User.deleteMany({
where: { isActive: false },
});
// count: number (number of deleted records)With Complex Filter
const count = await client.db.Post.deleteMany({
where: {
createdAt: { lt: new Date('2024-01-01') },
content: { isNull: true },
},
});
// count: numberMultiple Conditions
const count = await client.db.Session.deleteMany({
where: {
expiresAt: { lt: new Date() },
isRevoked: true,
},
});Logical Combinators
Use AND, OR, and NOT for complex deletion criteria:
const count = await client.db.Post.deleteMany({
where: {
OR: [
{ content: { isNull: true } },
{ createdAt: { lt: new Date('2023-01-01') } },
],
},
});Nested Relation Filters
You can filter through relations to delete records based on related data:
// Delete all posts where the author is inactive
const count = await client.db.Post.deleteMany({
where: {
author: { isActive: false },
},
});// Delete all users who have no posts
const count = await client.db.User.deleteMany({
where: {
posts: { isEmpty: true },
},
});Cascade Behavior
When records are deleted, Cerial automatically handles related records based on their relationship configuration:
- Required foreign keys pointing to the deleted records trigger auto-cascade deletion. If a
Posthas a requiredauthorIdand the referencedUseris deleted, thePostis also deleted. - Optional foreign keys follow their
@onDeletestrategy (e.g., set to null, set to NONE, or cascade). Configure this with@onDelete(SetNull),@onDelete(SetNone), or@onDelete(Cascade)on the relation field. - Array foreign keys have the deleted record's ID automatically removed from the array.
This cascade logic runs within the same transaction as the delete operation, ensuring data consistency. You don't need to manually clean up related records.
Be careful with broad where clauses — cascade deletions can propagate widely. A single deleteMany that removes many parent records could cascade-delete a large number of child records.
Return Value
- Returns a
numberindicating how many records were deleted. - Returns
0if no records match thewhereclause.
| Scenario | Return |
|---|---|
| Records matched | number (count of deleted records) |
| No match | 0 |