Filtered bulk cleanup
Remove records matching conditions such as an email substring, status, date range, or other model fields.
Prisma · Module
deleteMany removes multiple records matching an optional filter. Prisma executes it transactionally and reports how many records were deleted.
Call a model's deleteMany method with an optional where filter. Prisma runs the bulk deletion transactionally and returns a count of deleted records; an empty filter targets every record of that model.
prisma.<model>.deleteMany({ where?: <Model>WhereInput })| Name | Type | Description |
|---|---|---|
where | UserWhereInput (model-specific WhereInput) | Optional filter containing model fields and conditions that determine which records are deleted. |
BatchPayload containing the number of deleted records.
deleteMany method when one operation should remove multiple matching records.where filter to select records by model fields and conditions.deleteMany can remove related records matching a filter.$transaction, arrange them in an order that satisfies the relation constraints; dependent records may need deletion first.import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
const result = await prisma.user.deleteMany({
where: { email: { contains: "prisma.io" } },
});
console.log(`Deleted ${result.count} users`);
}
main().finally(() => prisma.$disconnect());Deletes every User whose email contains prisma.io and reports the number removed.
Remove records matching conditions such as an email substring, status, date range, or other model fields.
Clear a model during test teardown when the schema and relation order are known.
Delete dependent records across several models in a coordinated operation.
deleteMany do, and what does it return?deleteMany removes all records matching the optional where filter and returns a BatchPayload containing the deleted count. The operation runs in a transaction.
Follow-up: How would you delete related records safely? Use separate deleteMany calls in dependency order, or combine them in $transaction so the operations are coordinated atomically.
An empty filter can delete every record for the selected model. A where condition narrows the operation to matching records, such as users whose email contains a particular string.
Follow-up: What risk does an omitted filter create? It can remove the entire model's dataset, so construct and validate the filter deliberately before executing it.
Multiple deleteMany operations can be placed in $transaction([]). Prisma executes the batch as a transaction, and dependent deletions should be ordered so child records are removed before parent records.
Follow-up: What happens if one operation fails? The transaction rolls back the entire operation, preserving atomicity.
deleteupdateManycreateMany$transactionUserWhereInput