# Add Account Deletion to a Supabase App from a Linear Issue

Yes. An agent picks up the Linear issue and adds a confirmed delete-account route to your Supabase app, with storage cleanup, then opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-account-deletion/supabase/). 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 Linear issue and the repository, and maps what a user owns before it deletes anything. In a template like Razikus/supabase-nextjs-template, `supabase/migrations/20250130181641_todo_list.sql` creates `public.todo_list` with an `owner uuid` column that references `auth.users(id)` with `ON DELETE CASCADE` and a policy `Owner can do everything` gated on `authenticative.is_user_authenticated()` and `owner = auth.uid()`; `20250130165844_example_storage.sql` creates a private `files` bucket, and `20250130181110_storage_policies.sql` grants each user access to objects whose name starts with their own `auth.uid()` followed by `/`. `20250107210416_MFA.sql` defines that `authenticative` schema and its function, which requires `aal2` for users with a verified TOTP factor. In `nextjs/`, `src/lib/supabase/unified.ts` has a `SassClient` with `uploadFile`, `getFiles`, `deleteFile` and `shareFile` all under the `files` bucket and a `<userId>/` prefix, `server.ts` builds a cookie-aware server client with `@supabase/ssr`, and `serverAdminClient.ts` builds a client from `PRIVATE_SUPABASE_SERVICE_KEY` with no cookies and no session persistence. The only API route is `src/app/api/auth/callback/route.ts`, and `src/app/app/user-settings/page.tsx` offers a password change and `MFASetup` but no way to delete the account.

Deleting an `auth.users` row cascades to `todo_list`, but Storage objects do not cascade, so the agent writes the flow in a fixed order. It adds `src/app/api/account/delete/route.ts` as a `POST` handler that reads the caller with the SSR client's `auth.getUser()`, refuses when there is no session, and for a user with a verified MFA factor refuses unless `getAuthenticatorAssuranceLevel()` reports `aal2`, matching what `authenticative.is_user_authenticated()` demands of every other write. It then uses the admin client to list `files` objects under the user's `<id>/` prefix with `storage.from('files').list(id)` and remove them in pages, calls `auth.admin.deleteUser(id)`, which cascades the `todo_list` rows, and finally signs the browser out with `scope: 'global'` so other devices lose their sessions. The handler returns 204 and never echoes the service key or the user id in an error.

On the client it adds a `DeleteAccountCard` to `user-settings/page.tsx` beneath the password and MFA cards, using the existing `Card` and `Alert` components: a short warning that lists what will be removed (todos, files, sign-in), a confirmation input where the user types `DELETE`, and a button that posts to the new route and then redirects to `/auth/login` through the same pattern `SassClient.logout` uses. Because the template reads `useGlobal()` for the current user, the card shows the user's email in the warning so the right account is unmistakable.

In its sandbox the agent installs Docker first (`sudo apt-get install -y docker.io`, then starts `dockerd`), because `supabase start` runs the local stack in containers; it then runs the Supabase CLI against `supabase/config.toml`, starts the stack, applies the migrations, creates a user, uploads a file under its prefix, inserts a todo, calls the route with that user's session and checks that the `auth.users` row, the `todo_list` row and the storage object are gone; it then repeats the call for a second user to confirm the first user's deletion touched nothing else. It runs `yarn lint` and `yarn build` in `nextjs/`, since those are the scripts `package.json` has, and the pull request links the Linear issue and pastes the verification queries and their results.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "A Linear issue labelled agent says: \"Users need to delete their own account from Settings. Remove their files, their data and their sign-in, and ask them to confirm first.\" Do the work in this Supabase app. Read the migrations under supabase/, the Supabase client files under src/lib/supabase, the existing API routes and the user settings page first. Add a POST API route that checks the caller's session and, for users with a verified MFA factor, requires aal2; then with the service-role client remove the user's objects in the files bucket under their id prefix, call auth.admin.deleteUser, and sign the user out everywhere. Add a confirmation card to the settings page that requires typing DELETE. Never expose the service key to the browser. The sandbox has no Docker: install docker.io and start dockerd, then start the local Supabase stack, apply the migrations, create two users with a file and a todo each, delete one through the route and show with queries that only that user's rows and objects are gone. Run the project's lint and build scripts and open a pull request that links 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 that adds `src/app/api/account/delete/route.ts` and a `DeleteAccountCard` component, edits `src/app/app/user-settings/page.tsx` to render it, and adds a `deleteMyFiles` helper next to the other `files` bucket methods in `unified.ts` if the agent needed it on the server side. No migration is added, because `todo_list` already cascades on `auth.users` deletion and storage cleanup belongs in the route. The Linear issue is linked.

The description quotes the sandbox verification: the `select` over `auth.users`, `public.todo_list` and `storage.objects` before and after the call for both users, the route's response codes for an anonymous call, an `aal1` call from an MFA user and the successful call, and the `yarn lint` and `yarn build` output. It names the environment variable the route reads, `PRIVATE_SUPABASE_SERVICE_KEY`, which `serverAdminClient.ts` already expects.

Before merging, a reviewer checks that the route lives under a path their middleware matcher covers, that the service key is set only on the server, that the order of operations suits them (files first, then the user), and that any table they added since the template still cascades or is handled. They also decide whether to keep a short grace period instead of immediate deletion, which would be a schedule rather than this route.

## Run it on every Linear issue labelled agent

In the console, an automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` starts one run per labelled issue, with the issue added after the prompt, in the Project's repository. Changes made by an app integration never fire it, so the agent's own comment on the issue does not start another run. Each run reads that app's own migrations and bucket names, so an app with avatars in a second bucket or a `profiles` table without a cascade gets those handled and named in its pull request.

From your own server, the same run is one `POST /o/{org}/sessions` per issue: put the repository in `resources` and add the issue's `https://linear.app/<workspace>/issue/<ID>` URL there too, so Rush reads it through your Linear connection into the prompt. A webhook URL on the API key reports `run.completed` or `run.failed` with the `session_url` when the turn finishes.

## Why run it on Rush?

Account deletion is a feature where the test matters more than the code: the only way to know the storage objects, the rows and the session all went, and that nobody else's did, is to create users, delete one and query. In its own sandbox the agent runs the local Supabase stack from your `config.toml`, applies your migrations, and runs exactly that experiment, with every query and its result kept in the run's record and quoted in the pull request.

A Linear label becomes a reviewed, tested feature on its own branch, with the MFA and service-key handling that app stores and privacy regulators ask about written down. Several such issues can run at once, each in its own sandbox, each with its own pull request.

## Frequently asked questions

### Why does the route use the service-role key?

Because `auth.admin.deleteUser` and listing another prefix in Storage need it, and because the row-level policies that protect `todo_list` and `files` are written for the signed-in user, not for cleanup. The key is read on the server from `PRIVATE_SUPABASE_SERVICE_KEY`, which `serverAdminClient.ts` already uses, and never reaches the browser.

### What about MFA users?

The template's `authenticative.is_user_authenticated()` requires `aal2` for anyone with a verified TOTP factor, so the route applies the same rule: a session at `aal1` is refused and the card sends the user through the existing `/auth/2fa` page first. Deleting an account is at least as sensitive as any write the policies protect.

### Does it add a migration?

Not for this template, because `todo_list.owner` already references `auth.users(id)` with `ON DELETE CASCADE`. If your app has a table that references users without a cascade, the agent adds a migration for it and shows the orphan check that proved it was needed.

### Can deletion be delayed instead of immediate?

The issue asks for deletion, so the route deletes at once. A grace period is a different design: a `deleted_at` mark, a daily job and a cancel link. The pull request mentions it as an option and the agent can build it in a follow-up run if the team prefers that.

## More Supabase jobs

- [Add an Audit Log of Row Changes to a Supabase Project](https://getrush.ai/run/add-audit-log/supabase/)
- [Schedule Database Jobs With pg_cron on Supabase](https://getrush.ai/run/add-cron-jobs/supabase/)
- [Add Missing Indexes to a Supabase Database](https://getrush.ai/run/add-database-indexes/supabase/)

## The same job on other stacks

- [Add Account Deletion to an AdonisJS API](https://getrush.ai/run/add-account-deletion/adonisjs/)
- [Add Account Deletion to a Next.js App on Drizzle](https://getrush.ai/run/add-account-deletion/drizzle/)
- [Add Account Deletion to a Next.js and Prisma App](https://getrush.ai/run/add-account-deletion/prisma/)

## Keep reading

- [What an agent can do in Supabase itself, once you connect it](https://getrush.ai/integrations/supabase/)
- [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/)
