# Add Soft Delete to a Prisma Schema

Yes. An agent adds a deletedAt column with a migration, turns delete into update through a Prisma Client extension, filters reads, tests it and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-soft-delete/prisma/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the schema and every place the client is used before it decides how to intercept deletes. In a project like the `orm/express` example in prisma/prisma-examples, `prisma/schema.prisma` uses the `prisma-client` generator with `output = "./generated"`, a `postgresql` datasource, and two models: `User` with a unique `email` and `Post` with `createdAt`, `updatedAt`, `title`, `content`, `published`, `viewCount` and an optional `author` relation. `package.json` pins Prisma 7 with `@prisma/adapter-pg` and `pg`, Express 5 and `tsx`, and has a single `dev` script and no tests. `prisma.config.ts` sets the schema path, the `prisma/migrations` folder, the seed command `tsx prisma/seed.ts` and the datasource URL from `DATABASE_URL`; the `prisma/` folder holds only the schema and the seed, no migrations yet. `src/index.ts` builds one `PrismaClient` over a `PrismaPg` pool and serves `DELETE /post/:id` with `prisma.post.delete`, `GET /post/:id` with `findUnique`, `GET /feed` and `GET /user/:id/drafts` with `findMany`.

Prisma 7 removed the `$use` middleware, so the change is a client extension rather than a hook. The agent adds `deletedAt DateTime?` to `Post` with an index, starts Postgres in its sandbox, points `DATABASE_URL` at it and runs `prisma migrate dev --name post_soft_delete`, which creates the first file under `prisma/migrations`; because `migrate dev` no longer regenerates the client in Prisma 7, it runs `prisma generate` after it. The extended client lives in a new `src/prisma.ts`: `new PrismaClient({ adapter }).$extends({ query: { post: { ... } } })` turns `delete` into `update` with `deletedAt: new Date()` and `deleteMany` into `updateMany`, and adds `deletedAt: null` to `args.where` on `findMany`, `findFirst`, `findUnique`, `count` and `update` unless the caller already set `deletedAt` itself. Since Prisma 5, `findUnique` accepts non-unique fields next to the unique one, so `GET /post/:id` keeps its `findUnique` call.

`src/index.ts` imports the extended client from `src/prisma.ts`, keeps every route as it was, and gains `POST /post/:id/restore`, which sets `deletedAt` back to `null` through the one path the extension leaves open: a `where` that names `deletedAt` explicitly. `prisma/seed.ts` builds its own bare client and clears tables with `deleteMany` before seeding, which must stay a hard delete, so it is left on the bare client and the pull request says why.

Because the example has no test script, the agent adds `vitest` and a `test` script, and writes `src/soft-delete.test.ts` against the local database: create a user and a post, delete the post through the extended client, assert that `findMany` and `findUnique` no longer return it and that `count` with `deletedAt: { not: null }` still finds the row, then restore it and read it back. It runs `npx prisma migrate deploy`, `npx prisma db seed`, the new tests and `npx tsc --noEmit`, and starts `npm run dev` once to exercise `DELETE /post/:id` and `GET /feed` with curl.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add soft delete for the Post model in this Prisma 7 project. Read package.json, prisma.config.ts, prisma/schema.prisma, prisma/seed.ts, src/index.ts and tsconfig.json first. Add a nullable deletedAt DateTime column with an index to Post, start a local Postgres, set DATABASE_URL, run prisma migrate dev --name post_soft_delete and then prisma generate. Create src/prisma.ts exporting a client extended with $extends whose query extension on post turns delete into update and deleteMany into updateMany setting deletedAt, and adds deletedAt: null to the where of findMany, findFirst, findUnique, count and update unless the caller already filters on deletedAt. Use it in src/index.ts, keep every route, and add POST /post/:id/restore. Leave prisma/seed.ts on the bare client so its deleteMany stays a hard delete, and say why in the PR. Add vitest and a test script, write src/soft-delete.test.ts covering delete, the filtered reads, the count of deleted rows and restore, run prisma migrate deploy, prisma db seed, the tests and tsc --noEmit, and open a pull request that lists the operations the extension intercepts and the ones it does not.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `prisma/schema.prisma`, adds a migration under `prisma/migrations`, adds `src/prisma.ts` and `src/soft-delete.test.ts`, edits `src/index.ts` for the import and the restore route, and adds `vitest` with a `test` script to `package.json`. The description lists the operations the extension intercepts and, just as clearly, the ones it does not: nested relation reads such as `include: { posts: true }` on a user, raw SQL, and the seed script.

The run's record keeps the `migrate dev`, `generate`, `migrate deploy`, `db seed`, `vitest` and `tsc` output and the curl calls against the dev server, so a reviewer can see the migration apply cleanly on an empty database and the deleted post disappear from `/feed` without rebuilding the setup locally.

Before merging, a reviewer checks the migration against production data, since adding a nullable column is safe but the index name and the order of deploy and code release still matter, and decides whether `User` needs the same treatment.

## Run it on every Prisma model that must keep deleted rows

Teams usually need this one model at a time, as an audit or a support request arrives. From your own server, send one `POST /o/{org}/sessions` per model with the model name written into this prompt and the repository in `resources`, and a `request_id` that is a UUID v5 of the repository and model name together, so a retried call returns the run it started rather than a second migration.

In the console, the same prompt saved as an automation on the Project with a `linear` trigger on `linear.issue.labeled` and a `soft-delete` label puts the issue after the prompt; the run reads which model the issue names, adds the column and the extension for it, and opens a pull request that links the issue.

## Why run it on Rush?

Soft delete fails quietly: a `findUnique` that skips the filter, a `deleteMany` in a seed that becomes an update, a relation read that still returns deleted rows. In a Rush sandbox the agent runs a real Postgres, applies the real migration, and executes the delete, the reads and the restore through the extended client, so each of those cases is observed before the pull request opens, and the ones it cannot cover are written down.

Every command and its output stay in the run, so the reviewer reads the migration log and the test output rather than reproducing them, and the same request can go to several services that share the schema at once, each in its own sandbox.

## Frequently asked questions

### Why not Prisma middleware with $use?

Prisma 7 removed the client middleware API; `$extends` with a `query` extension is the replacement. The extension receives `args` and `query` per operation, so it can rewrite `delete` into `update` and add a `where` condition without touching the routes.

### Does the filter apply to relations and raw queries?

No. A `query` extension intercepts top-level operations on `post`; a user's `posts` loaded through `include` and anything through `$queryRaw` are not filtered. The agent searches the code for both, adds an explicit `where: { deletedAt: null }` where it finds an include, and lists the raw calls in the pull request.

### Can I still hard delete a row?

Yes, through the bare client that `prisma/seed.ts` uses, or through `$executeRaw`. The pull request keeps one place for that on purpose, so a reader can see that a hard delete is deliberate.

### What about the unique email on User if I soft delete users later?

A soft-deleted user still holds the unique `email`, so a new signup with the same address fails. The pull request flags this for `User` as the reason it stops at `Post`; the usual answers are a partial unique index or renaming the email on delete, and both are a separate decision.

## More Prisma jobs

- [Run Prisma Tests Against Their Own Database, Not the Dev One](https://getrush.ai/run/add-test-database-isolation/prisma/)
- [Add Transactions to a Prisma App](https://getrush.ai/run/add-transactions/prisma/)
- [Fix updatedAt Columns That Never Update in a Prisma Schema](https://getrush.ai/run/add-updated-at-timestamps/prisma/)

## The same job on other stacks

- [Add Soft Delete to a Quarkus Panache Entity](https://getrush.ai/run/add-soft-delete/quarkus/)
- [Add Soft Delete to Sequelize Models in an Express App](https://getrush.ai/run/add-soft-delete/sequelize/)
- [Add Soft Delete to SQLAlchemy Models](https://getrush.ai/run/add-soft-delete/sqlalchemy/)

## Keep reading

- [What an agent can do in Prisma itself, once you connect it](https://getrush.ai/integrations/prisma/)
- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
