# Add Soft Delete to a Fastify API from a Linear Issue

Yes. An agent reads the labelled Linear issue, adds a deleted_at column and a restore route to the Fastify tasks API with tests, and opens a linked pull request.

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

## What the agent does

The agent starts from the issue text and the repository as it is. In an API shaped like fastify/demo it reads `package.json` and finds Fastify 5.10 with TypeScript 6.0, Knex 3.3 on `mysql2`, Postgrator for migrations and a `test` script that seeds the database and runs `tsx --test` under c8. It reads the five numbered migrations, `001.do.users.sql` through `005.do.user_roles.sql`, each with its `.undo` twin, and `002.do.tasks.sql`, where the `tasks` table has `id`, `name`, `author_id`, `assigned_user_id`, `filename`, `status`, `created_at` and `updated_at` and no column that records a deletion. Then it reads `src/plugins/app/tasks/tasks-repository.ts`, where `delete` runs `knex('tasks').where({ id }).delete()` and `paginate`, `findById`, `findByFilename` and `createStream` all read the table with no deletion filter.

The change starts with a migration pair, `006.do.tasks_deleted_at.sql` adding a nullable `deleted_at TIMESTAMP` column to `tasks` and `006.undo.tasks_deleted_at.sql` dropping it, so `npm run db:migrate` and the CI job both pick it up in order. In the repository the agent turns `delete` into an update that sets `deleted_at` to the current time where it is still null, returning the affected row count the route already checks for 404, and adds a `restore` method that clears it. `paginate`, `findById`, `findByFilename` and the `createStream` behind `GET /api/tasks/download/csv` gain a `whereNull('deleted_at')` so a deleted task disappears from lists, lookups, image downloads and the CSV export alike.

In `src/routes/api/tasks/index.ts` the `DELETE /:id` handler keeps its shape and its 204, because it still calls `tasksRepository.delete`, and a new `POST /:id/restore` route returns the restored task through the existing `TaskSchema`, or 404 when there is nothing to restore. In `src/schemas/tasks.ts` the agent adds an optional `deleted_at` to `TaskSchema` so the response schema does not strip it, and leaves `CreateTaskSchema` and `UpdateTaskSchema` alone so a client cannot set the column by hand. The `archived` status in `TaskStatusEnum` stays what it is: a workflow state, not a deletion.

The tests change where the behaviour changed. `test/routes/api/tasks/tasks.test.ts` already has a `DELETE /api/tasks/:id` block whose `should delete an existing task` case reads the row back and expects `undefined`; the agent rewrites that assertion to expect a non-null `deleted_at`, adds a case that the deleted task no longer appears in `GET /api/tasks`, and adds a `POST /api/tasks/:id/restore` block with a success case and a 404 case, all through the `injectWithLogin` helper in `test/helper.ts` that logs in with the seeded users. It runs `npm run lint` and `npm run test` against the MySQL 8.4 container that `docker-compose.yml` defines, fixes what fails, and only then pushes a branch and opens the pull request with the issue linked in its description.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "A Linear issue labelled agent says: \"Add soft delete to tasks. DELETE /api/tasks/:id should set a deleted_at timestamp instead of removing the row, every read including the CSV export should skip deleted tasks, and POST /api/tasks/:id/restore should bring one back.\" Add the Postgrator migration pair, update the tasks repository, routes and schemas, update and extend the tests in test/routes/api/tasks, run npm run lint and npm run test, and open a pull request that links back to the issue.",
  "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 on its own branch. Its diff adds `migrations/006.do.tasks_deleted_at.sql` and its `.undo` file, changes `src/plugins/app/tasks/tasks-repository.ts` for the filtered reads, the soft `delete` and the new `restore`, changes `src/routes/api/tasks/index.ts` for the restore route, touches `src/schemas/tasks.ts` for the optional `deleted_at` field, and rewrites the delete block of `test/routes/api/tasks/tasks.test.ts` with the restore cases beside it. The description quotes the issue and lists which reads now exclude deleted rows.

The repository's own CI is the proof the reviewer did not have to produce. `.github/workflows/ci.yml` starts a MySQL 8.4 service and runs the lint and test steps on Node 22 and Node 24 for every pull request, so the new migration runs and the new cases execute there, not only in the sandbox. The session's trajectory, read at `GET /o/{org}/sessions/{id}/messages`, keeps every `npm run test` the agent ran with its full output.

Before merging, a reviewer checks three things: that `findByFilename` and the CSV stream carry the `whereNull('deleted_at')` filter, since those are the reads a client could still reach for a deleted task; that `restore` is reachable only by a logged-in user, which the `onRequest` hook in `src/routes/api/autohooks.ts` already enforces for everything under `/api` except login; and that no part of the diff changed the `archived` status. Nothing reaches `main` until the pull request merges.

## Run it on every Linear issue labelled agent

Sign in to Linear once from the console, which creates a `linear` app connection on your account. Then add an automation in the Project that holds this repository with the prompt above, minus the quoted issue, and a `linear` trigger on `linear.issue.labeled` for the `agent` label. Each labelled issue starts one run with the issue as its context after the prompt, and each run opens its own pull request that links back, so a backlog of small API changes like this one turns into a queue of reviewable branches without anyone checking the repository out.

The Rush GitHub App must be installed on the repository first, the one step the API does not do. An automation run has its own `max_runs_per_hour` cap, so a bulk relabel in Linear is spread out rather than refused. Changes made by an app integration never fire the trigger, so a run that comments on its own issue does not start another.

## Why run it on Rush?

Soft delete is a change that touches a migration, a repository, three routes, a schema and a test file, and the only way to know it is right is to run the suite against a real MySQL. On a laptop that means Docker running, `.env` filled in, `npm run db:create` and `npm run db:migrate` done before the first test, and a working tree you cannot use for anything else until the branch is pushed. On Rush the run gets its own sandbox with a computer in it: the agent brings up the database from `docker-compose.yml`, applies the migration, runs `npm run test` as many times as it takes, and pushes only when it passes.

The result is a pull request with evidence, not a summary. The diff shows exactly which reads gained the filter, CI runs the suite on two Node versions, and the trajectory keeps every command and its output so a reviewer can see the delete test fail before the fix and pass after it. Several labelled issues become several sandboxes and several pull requests at once, each isolated from the others.

## Frequently asked questions

### Will the DELETE route's contract change?

No. `DELETE /api/tasks/:id` keeps answering 204 on success and 404 when the task does not exist, because the route still calls `tasksRepository.delete` and still checks the affected row count. What changes is what the repository does underneath: it sets `deleted_at` instead of removing the row, and a second delete of the same task answers 404 because the `whereNull('deleted_at')` condition no longer matches.

### Does the CSV export skip deleted tasks?

Yes. `GET /api/tasks/download/csv` streams `tasksRepository.createStream()` through `csv-stringify` and gzip, so the filter is added to that query in the repository rather than in the route. A reviewer can confirm it in the diff of `tasks-repository.ts`, and the test file gains a case that a deleted task is absent from the list endpoint.

### How does the migration run in CI?

The repository's `ci.yml` runs the `test` script, which seeds the database through the same Postgrator setup `scripts/migrate.ts` uses, with `migrationPattern` matching every file in `migrations/`. A new pair named `006.do.tasks_deleted_at.sql` and `006.undo.tasks_deleted_at.sql` follows the existing numbering, so Postgrator applies it after `005` on both the Node 22 and Node 24 jobs.

### What about the task's uploaded image?

`GET /api/tasks/:filename/image` looks the task up with `findByFilename`, which gains the same filter, so a deleted task's image answers 404 like the task itself. The file stays on disk under the `tasksFileManager`, so restoring the task brings the image back without re-uploading it.

### Can I ask for a hard delete after a retention period instead?

Yes, in the issue text. The prompt is the issue, so an issue that says deleted tasks should be purged after 30 days gets a purge query in the repository and a note in the pull request on where to schedule it. The agent does not invent a retention rule the issue did not state.

## More Fastify jobs

- [Add User Registration to a Fastify App](https://getrush.ai/run/add-user-registration/fastify/)
- [Add WebSockets to a Fastify App](https://getrush.ai/run/add-websockets/fastify/)
- [Dockerize a Fastify App With MySQL](https://getrush.ai/run/dockerize-app/fastify/)

## The same job on other stacks

- [Add Soft Deletes to a Filament Resource](https://getrush.ai/run/add-soft-delete/filament/)
- [Add Soft Delete to GORM Models](https://getrush.ai/run/add-soft-delete/gorm/)
- [Add Soft Delete to a Koa API](https://getrush.ai/run/add-soft-delete/koa/)

## Keep reading

- [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/)
