---
title: delete
description: Remove records from your database
icon: trash-2
---

`delete` removes a single record matched by a unique identifier and returns it. It throws if the record doesn't exist.

```ts
const user = await client.user.delete({
  where: { id: "user_123" },
});
// Result: the deleted record
```

## Options

```ts
await client.user.delete({
  where: { ... },       // Required: unique identifier
  select: { ... },      // Optional: fields to return
  include: { ... },     // Optional: relations to include
});
```

`select` and `include` control the returned shape — see [Selecting Fields](/docs/client/selecting).

## Conditional Deletes

The `where` may narrow the unique lookup with non-unique scalar filters and
`AND` / `OR` / `NOT`:

```ts
// delete this token, but only if it has already expired
await client.token.delete({
  where: { id: "tok_1", expiresAt: { lt: new Date() } },
});
```

If the unique key matches but the filter excludes the row, the delete is a
`NotFoundError` and the row survives. See
[Narrowing a Unique Lookup](/docs/client/find-unique#narrowing-a-unique-lookup)
for the full `where` shape.

## Cascading Deletes

When a record is deleted, related records are handled based on `onDelete`:

```ts
const post = s.model({
  authorId: s.string(),
  author: s
    .toOne(() => user)
    .fields("authorId")
    .references("id")
    .onDelete("cascade"), // Delete posts when user is deleted
});
```

| Action | Behavior |
|--------|----------|
| `cascade` | Delete related records |
| `setNull` | Set FK to null |
| `restrict` | Prevent deletion |
| `noAction` | Database default |

## Soft Delete Pattern

To keep deleted rows and hide them from queries, use the official
[soft-delete extension](/docs/extensions/soft-delete): `delete` then writes a
`deletedAt` time instead of removing the row, and every read, count and
relation skips it.

```ts
import { softDelete } from "viborm/soft-delete";

const post = s.model({
  id: s.string().id(),
  title: s.string(),
  deletedAt: s.dateTime().nullable(),
});

const db = softDelete({ models: { post: { deletedAt: "deletedAt" } } })(client);

await db.post.delete({ where: { id } });      // sets deletedAt
await db.post.findMany();                      // live posts only
await db.post.restore({ where: { id } });      // clears deletedAt
```

## Examples

### Delete User Account

```ts
async function deleteAccount(userId: string) {
  // Delete in correct order due to FK constraints
  await client.post.deleteMany({
    where: { authorId: userId },
  });

  await client.profile.delete({
    where: { userId },
  }).catch(() => {}); // Ignore if no profile

  return client.user.delete({
    where: { id: userId },
  });
}
```
