# Add Soft Delete to Sequelize Models in an Express App

Yes. An agent turns on paranoid mode for your Sequelize models, makes DELETE routes answer 404 when nothing matched, adds a restore route, tests it, and opens a PR.

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

## What the agent does

The agent reads how rows are deleted today before it changes what delete means. In an app like the `express-main-example` in sequelize/express-example, `sequelize/index.js` builds one `Sequelize` instance on SQLite with `storage: 'sqlite-example-database/example-db.sqlite'`, `logQueryParameters: true` and `benchmark: true`, defines three models from `sequelize/models/` and runs `extra-setup.js`, where `orchestra.hasMany(instrument)` and `instrument.belongsTo(orchestra)` add an `orchestraId` column. None of the three `sequelize.define` calls sets `timestamps: false`, so every table already has `createdAt` and `updatedAt`, which is what paranoid mode needs. `express/app.js` wires each controller's `getAll`, `getById`, `create`, `update` and `remove` to `/api/<route>` and `/api/<route>/:id` through `makeHandlerAwareOfAsyncErrors`, and each `remove` in `express/routes/users.js`, `instruments.js` and `orchestras.js` calls `destroy({ where: { id } })` and answers `200` whether or not a row existed. `sqlite-example-database/setup.js` rebuilds the file with `sequelize.sync({ force: true })` and seeds four users, three orchestras and ten random instruments each. `package.json` has `sequelize` `^6.3.3`, `express` `^4.17.1`, `sqlite3` `^5.0.0`, `engines` of `node >=10`, no test script and no dev dependencies, and `.npmrc` sets `package-lock=false`.

The agent adds `paranoid: true` to the options of each model the prompt names, so `destroy` becomes an `UPDATE` that sets `deletedAt` and every `findAll`, `findByPk` and `findAndCountAll` skips rows where it is set. Because the schema here comes from `sync({ force: true })`, the `deletedAt` column appears the next time `npm run setup-example-db` runs, and the pull request says so; a project that has moved to `sequelize-cli` migrations gets a migration that adds a nullable `deletedAt` to each table instead. In `sequelize/index.js` the storage path becomes `process.env.DB_STORAGE` with the current file as the default, so tests can run on `:memory:` without touching the example database.

The routes change in two places. `remove` reads the number `destroy` returns and answers `404` when it is zero, which the current code never does; a second call on the same id is therefore a 404 rather than a silent 200, and the description explains that change in behaviour. A `restore` function is added to each controller and `express/app.js` gains one more block in its loop, `POST /api/<route>/:id/restore`, which calls `Model.restore({ where: { id } })`; a hard delete stays out of the API, because `destroy({ force: true })` is the kind of call a reviewer should add on purpose. One consequence is named rather than hidden: `user.username` is `unique: true`, and a soft-deleted user keeps its row, so creating the same username again fails on the constraint until the old row is restored or force-deleted. The pull request states the options and leaves the constraint as it is.

Tests are new to the repository, so the agent adds `supertest` as a dev dependency, a `test` script that runs `node --test`, and `test/soft-delete.test.js`, which starts the Express app on an in-memory database, syncs the models, and checks that `DELETE /api/users/1` answers 200 and `GET /api/users/1` then answers 404, that `findByPk(1, { paranoid: false })` still finds the row with `deletedAt` set, that the restore route brings it back, that a second delete answers 404, and that `GET /api/orchestras?includeInstruments` no longer lists a deleted instrument. Because `node --test` needs a newer Node than `>=10`, the `engines` field is raised and the reason is in the description. The agent runs `npm install` and `npm test` in its sandbox and keeps the output.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add soft delete to the Sequelize models in this Express app. Read package.json, .npmrc, index.js, express/app.js, express/helpers.js, every file under express/routes/ and sequelize/, and sqlite-example-database/setup.js first. Set paranoid: true on the user, instrument and orchestra models, keep the default deletedAt column name, and explain in the PR that the column appears on the next setup-example-db run since the schema comes from sync({ force: true }). Make each remove handler answer 404 when destroy affected no rows, add a restore handler per controller wired as POST /api/<route>/:id/restore in app.js, and do not expose a force delete. Make the SQLite storage path overridable with DB_STORAGE so tests use :memory:. Name the unique username consequence in the PR without changing the constraint. Add supertest and a node --test suite covering delete, 404 after delete, paranoid: false lookup, restore, a repeated delete and the includeInstruments listing; raise engines as needed and say why. Run npm test, include the output, update the readme, and open a pull request.",
  "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 the three files under `sequelize/models/`, `sequelize/index.js`, `express/app.js`, `express/routes/users.js`, `instruments.js` and `orchestras.js`, `package.json` and `readme.md`, and adds `test/soft-delete.test.js`. `sqlite-example-database/setup.js` and `extra-setup.js` are unchanged.

The description lists the behaviour changes a client will see, the 404 on a missing or already deleted id and the new restore route, explains the username constraint, and quotes the `node --test` output. The commands and their full output are in the transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer checks whether any report or raw query reads these tables directly, since raw queries do not filter on `deletedAt`, and decides what to do about re-used usernames. A follow-up message to the same session can add a `?deleted=true` listing for admins or a force delete behind an auth check.

## Run it on every Express app whose DELETE routes remove rows for good

A team with several Sequelize services sends one `POST /o/{org}/sessions` per repository with this prompt as `input` and the repository in `resources`. Each run reads its own models, so one with `timestamps: false` on a model gets timestamps turned on for it first, and one that already uses migrations gets a migration rather than a note about `sync`.

Send a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run it already started. The key's `webhook_url` reports when each run the key starts finishes, with `session_url` for the pull request link and the test output.

## Why run it on Rush?

Soft delete looks like one option on a model, and the surprises are everywhere else: a delete that still answers 200 for a row that is gone, a unique index that now blocks re-registration, an include that may or may not hide deleted children. The agent reads every route and association before it flips the option and writes a test for each consequence, in a sandbox of its own, with the output kept.

The pull request carries the model change, the route changes and the tests together, so a reviewer reads one diff instead of discovering the 404 change from a client error. Several services can be done at once, each in its own sandbox with its own pull request.

## Frequently asked questions

### Will existing rows get a deletedAt value?

No. The column is nullable and existing rows stay `NULL`, which paranoid mode reads as not deleted. Only rows you delete after the change get a timestamp.

### What about the unique username?

A soft-deleted user still occupies its username, so creating it again fails on the unique constraint. The agent names this in the pull request and leaves the constraint alone; the fix you choose, restoring the old row, force-deleting it, or a different uniqueness rule, is a product decision.

### Can I still hard delete?

Yes, in code: `destroy({ force: true })` issues a real `DELETE`. The agent keeps it out of the HTTP API on purpose and shows the call in the description so you can add it behind authorization.

### Does includeInstruments hide deleted instruments?

The test suite checks exactly that on `GET /api/orchestras?includeInstruments` and the pull request reports the result rather than assuming it. If you need deleted children listed somewhere, pass `paranoid: false` on that include.

### Do I need a migration?

Not in a project that builds its schema with `sync({ force: true })` as this one does; re-running the setup script adds the column. If your project uses `sequelize-cli`, say so and the agent writes a migration that adds a nullable `deletedAt` to each paranoid table.

## More Sequelize jobs

- [Add Structured Logging to a Sequelize App](https://getrush.ai/run/add-structured-logging/sequelize/)
- [Add Transactions to a Sequelize App](https://getrush.ai/run/add-transactions/sequelize/)
- [Convert a Sequelize Express App to TypeScript](https://getrush.ai/run/convert-javascript-to-typescript/sequelize/)

## The same job on other stacks

- [Add Soft Delete to SQLAlchemy Models](https://getrush.ai/run/add-soft-delete/sqlalchemy/)
- [Add Soft Delete to a sqlc Project](https://getrush.ai/run/add-soft-delete/sqlc/)
- [Add Soft Delete to a SQLModel and FastAPI App](https://getrush.ai/run/add-soft-delete/sqlmodel/)

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