# Add Soft Delete to an Axum API

Yes. An agent adds a deleted_at column, turns the article delete into an update, hides deleted rows from every query, keeps the spec suite green, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-soft-delete/axum/). 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 query that touches it before it changes a delete. In an API shaped like launchbadge/realworld-axum-sqlx, `Cargo.toml` has `axum` 0.3.4, `sqlx` 0.5 with the `postgres`, `uuid` and `time` features, `tokio`, `tower-http` with `trace`, `argon2` and `jwt`. `migrations/4_article.sql` creates `article` with `slug text unique not null`, `tag_list text[]`, `created_at` and `updated_at`, a GIN index on `tag_list`, and the cousin tables `article_favorite` and `article_comment`, both `references article (article_id) on delete cascade`. `migrations/1_setup.sql` supplies the `trigger_updated_at` function the tables use. Every query is a `sqlx::query!` or `query_as!` macro checked against `DATABASE_URL` at compile time, so a column that is added to a `where` clause is verified by `cargo build`.

`src/http/articles/mod.rs` routes `/api/articles`, `/api/articles/feed`, `/api/articles/:slug` with `get`, `put` and `delete`, the favorite routes and `/api/tags`. Its `delete_article` is one statement: a `with deleted_article as (delete from article where slug = $1 and user_id = $2 returning 1)` CTE followed by two `exists` checks, `existed` and `deleted`, so one round trip decides between `404`, `403` and success. `get_article`, `update_article`, `favorite_article` and `article_by_id` each select `from article` by `slug` or `article_id`; `get_tags` unnests `tag_list` across the whole table. `src/http/articles/listing.rs` has `list_articles` with a `where (...)` block for `tag`, `author` and `favorited`, `order by article.created_at desc limit $5 offset $6`, and `feed_articles` joining `follow`. `src/http/articles/comments.rs` looks the article up by slug in `get_article_comments`, `add_comment` and `delete_comment`. `.github/workflows/rust.yml` starts Postgres, installs `sqlx-cli`, runs `sqlx db setup`, `cargo build` and `cargo test`, then runs the RealWorld Postman collection against `cargo run` on port 8080.

The agent writes `migrations/5_article_soft_delete.sql`: `alter table article add column deleted_at timestamptz`, drop the `slug` unique constraint and replace it with `create unique index article_slug_live on article (slug) where deleted_at is null`, so an author can publish a new article under a slug they deleted, plus a partial index on `(created_at desc) where deleted_at is null` for the listing. `delete_article` keeps its CTE shape and becomes `update article set deleted_at = now() where slug = $1 and user_id = $2 and deleted_at is null returning 1`, with the `existed` check reading live rows only, so the three outcomes are unchanged. Every other query gains `and article.deleted_at is null`: the four single-article selects, `list_articles`, `feed_articles`, `get_tags`, and the three slug lookups in `comments.rs`, so a deleted article's comments and favorites stop being reachable without touching those tables. The `Article` structs and JSON are unchanged, because the spec has no field for a deleted article.

An `ApiContext` already carries the `PgPool`, so the agent adds a `purge_deleted_articles` function in `src/http/articles/mod.rs` that deletes rows whose `deleted_at` is older than an interval, and a `--purge-deleted-articles` flag in `src/config.rs` and `src/main.rs`, read by `clap` beside `--database-url` and `--hmac-key`, which runs it once and exits, so the cascade to comments and favorites happens on an operator's schedule. Tests go beside the existing ones: an integration test in `tests/soft_delete.rs` that creates a user, publishes, deletes, asserts `GET /api/articles/:slug` is `404`, `GET /api/articles` and `/api/tags` no longer show it, a second publish with the same slug succeeds, and a direct `select deleted_at` shows the row is still there. The agent runs `sqlx db setup`, `cargo build`, `cargo test`, and the Postman collection the workflow runs, 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 for articles to this Axum and sqlx API and open a pull request. Read Cargo.toml, migrations/1_setup.sql, migrations/4_article.sql, src/http/mod.rs, src/http/articles/mod.rs, src/http/articles/listing.rs, src/http/articles/comments.rs, src/config.rs, src/main.rs and .github/workflows/rust.yml first. Add migrations/5_article_soft_delete.sql that adds a nullable deleted_at timestamptz to article, replaces the slug unique constraint with a partial unique index where deleted_at is null, and adds a partial index on created_at for live rows. Change delete_article to set deleted_at instead of deleting while keeping its CTE and its 404, 403 and success outcomes. Add a deleted_at is null condition to every article select in mod.rs, listing.rs and comments.rs, including get_tags, without changing the JSON. Add a purge function and a clap flag that deletes rows soft-deleted longer than a given interval and exits. Write an integration test under tests/ covering delete, 404, listing, tags, re-publishing the same slug and the row still existing. Run sqlx db setup, cargo build, cargo test and the RealWorld Postman collection as the workflow does, and paste the output in the 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 with one new migration, a changed `delete_article`, one added condition in each of the article queries across three files, a purge function and flag, and a new `tests/soft_delete.rs`. The description lists every query it touched with the file and function name, explains the partial unique index on `slug`, and pastes the `cargo build` and `cargo test` output and the Postman collection summary.

The repository's `Rust` workflow runs `sqlx db setup`, the build, the tests and the Postman collection against a Postgres service on the pull request, so the compile-time query checks and the spec suite run as checks with the new migration applied.

A reviewer checks that no article query is missing the new condition, that the purge interval is a flag rather than a constant, that `delete_article` still answers `403` for another author's article, and that favorites and comments of a deleted article are hidden rather than deleted until purge runs. They merge and apply the migration with `sqlx migrate run` on deploy.

## Run it on every API that deletes rows it may need back

A team with several Axum services sends one `POST /o/{org}/sessions` per repository with this prompt and the repository in `resources`. Each run reads that service's own tables and `sqlx` queries, so a service with orders instead of articles gets the column and conditions where its deletes are, and each run opens its own pull request.

Send a `request_id` with each call so a retry returns the run already started, and set a `webhook_url` on the key so `run.completed` arrives with `session_url` when the turn ends and your handler posts the pull request where the backend reviewers read it.

## Why run it on Rush?

Soft delete is a change where missing one query is a bug users find: a deleted article still in a tag count, or still in someone's feed. The compile-time checks in `sqlx::query!` need a live Postgres with the new migration applied, and the spec suite needs the server running. In a Rush sandbox the agent sets up that database, builds, runs both suites, and keeps every command and result in the run's record beside the pull request.

By hand this means a Postgres, `sqlx-cli`, a careful read of eleven queries and a Postman run. The agent does it in one pass with the repository's own workflow steps, and a follow-up such as "add a `GET /api/articles/:slug/restore` for the author" wakes the same parked sandbox and lands on the same branch.

## Frequently asked questions

### Why replace the slug unique constraint?

With a plain `unique` on `slug`, an author who deletes an article could never publish another with the same title, because the deleted row keeps the slug. A unique index `where deleted_at is null` enforces uniqueness among live articles only. The migration drops the constraint and creates the index in one file so both happen together.

### What happens to comments and favorites?

Nothing at delete time. They stay in their tables, unreachable because every route resolves the article first and now ignores deleted ones. When the purge flag runs, the hard delete of old `article` rows cascades to them through the existing `on delete cascade`, so a restore within the purge window brings them back.

### Does the API change for clients?

No. `DELETE /api/articles/:slug` still answers success, `404` or `403` in the same cases, and a deleted article is `404` everywhere afterwards. The RealWorld Postman collection in the workflow runs against the branch to show the spec behaviour held.

### Why a flag instead of a background task?

The service has no scheduler and `src/main.rs` already reads its configuration with `clap`. A `--purge-deleted-articles` run is one command an operator puts in a cron or a job, is easy to test, and adds no runtime dependency; a scheduled task inside the server would be a second thing to monitor.

## More Axum jobs

- [Add Structured Logging to an Axum API](https://getrush.ai/run/add-structured-logging/axum/)
- [Inventory Every API Endpoint of an Axum Service](https://getrush.ai/run/api-endpoint-inventory/axum/)
- [Dockerize an Axum API With SQLx and Postgres](https://getrush.ai/run/dockerize-app/axum/)

## The same job on other stacks

- [Add Soft Delete to a Django REST Framework API](https://getrush.ai/run/add-soft-delete/django-rest-framework/)
- [Add Soft Delete to a Drizzle Schema](https://getrush.ai/run/add-soft-delete/drizzle/)
- [Add Soft Delete to a Fastify API from a Linear Issue](https://getrush.ai/run/add-soft-delete/fastify/)

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