deleteMany
Delete every record matching a filter
Delete every record matching the filter, in one call:
const result = await client.post.deleteMany({
where: { published: false },
});
// Result: { count: 15 }
deleteMany returns a { count }.
Delete all
// Delete every record (use with care!)
await client.post.deleteMany({});
// or
await client.post.deleteMany();
// or
await client.post.deleteMany({ where: {} });
A where that matches every row only because its members are undefined is
refused, unlike Prisma without strictUndefinedChecks:
const tenantId: string | undefined = undefined;
await client.post.deleteMany({ where: { tenantId } });
// ValidationError: deleteMany would delete every row: its where names only
// undefined values (tenantId), and an undefined value is no condition. …
The same rule applies to a nested deleteMany / updateMany in a relation
write, where it would have changed every related row. See
Filter behavior for how
AND, OR, NOT and every are read.
Examples
// Delete old drafts
await client.post.deleteMany({
where: {
published: false,
createdAt: { lt: new Date("2023-01-01") },
},
});
// Delete by relation
await client.post.deleteMany({
where: {
author: { is: { status: "BANNED" } },
},
});
// Delete inactive non-admin users
await client.user.deleteMany({
where: {
lastLogin: { lt: new Date(Date.now() - 365 * 24 * 60 * 60 * 1000) },
role: { not: "ADMIN" },
},
});
Getting the deleted rows back
Add a select and deleteMany returns the rows it removed instead of a count:
const removed = await client.post.deleteMany({
where: { published: false },
select: { id: true, title: true },
});
// Type: { id: string; title: string }[]
The rows are captured and deleted inside one atomic scope, so what you get back is exactly what is now gone — no read-then-delete window where another writer can change the set in between.
Capping how many rows are deleted
limit caps the number of rows the delete removes. The count you get back is
min(matching, limit):
// Trim the audit log in bounded chunks instead of one huge delete
const result = await client.auditEntry.deleteMany({
where: { createdAt: { lt: cutoff } },
limit: 1000,
});
// Result: { count: 1000 } if at least 1000 matched, fewer otherwise
limit: 0 is legal and means “delete nothing”: you get { count: 0 } and no
write statement is sent at all. A negative or fractional limit is a validation
error. limit caps the returning form too — with select you get back exactly
the rows that were removed.
limit takes the first rows by id: the matching rows ordered by primary key,
ascending, as findMany with take and no orderBy
orders them. A compound primary key is ordered field by field, in the order the
model declares the fields. With limit: 2 over ids 5, 3, 1, 4 and 2, ids 1 and
2 are deleted, whatever order the rows were inserted in, on every database.
A bulk write takes no orderBy: to delete other rows first, read their ids
with findMany and delete by those ids.
Options
await client.user.deleteMany({
where: { ... }, // Optional: filter (all rows if empty)
limit: 1000, // Optional: cap on how many rows are deleted
select: { ... }, // Optional (scalar fields only): return the deleted rows
// instead of { count }
});