Delete by ID
Remove a user after locating it by its primary key.
Prisma · Function
Prisma Client's delete removes one existing record selected by ID or another unique attribute. It returns the deleted record, with select or include available to control the returned shape.
Call the model delegate's delete method with a where object identifying exactly one record by its ID or another unique field. Prisma removes that record and returns the deleted record; use select to restrict returned fields, while related-record behavior depends on the configured relation rules.
prisma.<model>.delete({ where: <ModelWhereUniqueInput>, select?: <ModelSelect>, include?: <ModelInclude> })| Name | Type | Description |
|---|---|---|
where | ModelWhereUniqueInput | Unique selector identifying the single record to delete; typically contains an ID or another unique field. |
select | ModelSelect | Optional field projection controlling which fields are returned from the deleted record. |
include | ModelInclude | Optional relation and field projection controlling the returned record shape. |
A Promise resolving to the deleted record, shaped by select or include; the operation throws if no uniquely matching record exists or referential constraints prevent deletion.
delete removes one existing database record.select when the caller needs only chosen fields from the deleted record.delete can remove a related record during a parent write.deleteMany instead of delete when the operation should target records matching a general filter rather than one uniquely identified record.delete removes the related child record only; it does not remove the parent record.const deletedUser = await prisma.user.delete({
where: { email: "elsa@prisma.io" },
select: { id: true, name: true, email: true },
});
console.log(deletedUser);Deletes the uniquely identified user and returns only the selected fields from the deleted record.
Remove a user after locating it by its primary key.
Remove a record identified by a unique email, username, or other unique field.
Remove a related child during a nested write without deleting its parent.
Return only fields needed by the caller after deletion.
delete operation do?delete removes one existing record identified through where, which can use an ID or another unique attribute. It returns the deleted record, and select can limit the returned fields.
Follow-up: How would you remove all users with a matching email domain? Use deleteMany with a filter, because delete requires a uniquely identifying condition.
delete target?The where input must identify a single record through a unique field, such as id or a unique email. A non-unique general criterion belongs with deleteMany.
Follow-up: What happens if several records satisfy a non-unique condition? delete is not the appropriate operation; use deleteMany to target the matching set.
A parent deletion can fail when required related records still exist, such as posts that require an author. Related records must be deleted first, or the schema must use appropriate referential actions such as cascading behavior.
Follow-up: How can multiple dependent deletions remain atomic? Put the separate delete operations in a transaction so they succeed or roll back together.
deleteManyselectincludewhere$transactionreferential actions