Soft Delete
Soft delete turns a delete into an update of the management field, and default queries no longer return those records.
Enabling
model Account {
id: int @id @auto
email: string @unique
deletedAt?: datetime
@@softDelete(deletedAt)
}2
3
4
5
6
7
@@softDelete() uses deletedAt and adds it when absent. An explicitly selected field must already exist, be nullable datetime or timestamp, default to null, and not be a primary or unique key.
Operations
Use delete to mark a record, restore to make it visible again, and forceDelete to remove it physically. Method results, bulk variants, and $clear are documented in Delete.
Query Visibility
The deleted option controls visibility for the current query's model:
await db.account.findMany({ deleted: "include" });
await db.account.findMany({ deleted: "only" });2
| Value | Behavior |
|---|---|
exclude | Omit deleted records, the default |
include | Also return deleted records |
only | Return only deleted records |
The default exclude applies to lists, unique queries, counts, aggregations, grouping, eager loading, and relation filters.
deleted only affects the current model. Relation filter targets always exclude deleted records; eager loading can be configured separately:
await db.account.findMany({
include: { entries: { deleted: "include" } },
});2
3
Write Rules
- The management field is maintained by the ORM: it cannot be assigned through
update, and new records cannot carry a deletion time. Ordinary updates do not touch deleted records, andupsertdoes not restore them. restoremust satisfy unique constraints; on conflict, the whole transaction rolls back.
Unique Constraints
Unique constraints still include deleted records, so a deleted record keeps occupying its unique key.
Relations
- Soft delete does not cascade; the physical foreign key still exists, and foreign key writes or
connectcannot point at a deleted target (SOFT_DELETED_REFERENCE). - Eager loading hides deleted targets by default: a missing required relation raises
SOFT_DELETED_RELATION, an optional one returnsnull. - A nested soft delete keeps the link so the record can be restored; explicit
disconnectandsetstill modify links.
Deleted records stay readable
Any caller can read deleted records via deleted: 'include' or raw SQL, and raw SQL does not apply the soft-delete policy.
Next Steps
- Delete: the delete operations and
$clear - Referential Actions: how deleted rows affect dependent records