All jobs

Add Account Deletion to a Next.js App on Drizzle

Yes. An agent adds a delete-account action that removes a user's chats, messages, votes, documents and suggestions in foreign-key order, then the user, in a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the schema for what a user owns and in which order it can go. In a Next.js app laid out like vercel/chatbot, lib/db/schema.ts has seven tables: User; Chat with userId referencing User; Message_v2, Vote_v2 and Stream referencing Chat (and Vote_v2 referencing Message_v2); Document with userId referencing User and a composite primary key of id and createdAt; and Suggestion referencing both User and Document by that composite key. Every foreign key is declared with .references() or foreignKey() and no onDelete, and 0000_initial.sql creates them with no ON DELETE clause, so Postgres refuses to delete a user that still has a chat or a document.

It then reads what the app already deletes. lib/db/queries.ts has deleteChatById, which removes votes, messages and streams before the chat, and deleteAllChatsByUserId, which collects the user's chat ids and does the same with inArray, both wrapped in ChatbotError("bad_request:database"). There is nothing for documents or suggestions, and nothing that deletes a User. app/(auth)/auth.ts builds the session from NextAuth with a jwt callback carrying id and type, where type is guest or regular; app/(auth)/actions.ts has login and register server actions; and components/chat/sign-out-form.tsx calls signOut.

The agent adds deleteUserById to queries.ts in the order the keys allow and inside one db.transaction: suggestions by userId, then suggestions on the user's documents, documents by userId, the chats through the same steps as deleteAllChatsByUserId, and last the User row, returning the counts. A deleteAccount server action in app/(auth)/actions.ts reads the session with auth(), refuses when there is none, calls the query with session.user.id, then signOut with a redirect to /. A Delete account entry goes in components/chat/sidebar-user-nav.tsx next to the sign-out, behind a confirmation, and it is not shown to a guest session, whose rows createGuestUser makes with no password to protect.

A new test joins tests/e2e/auth.test.ts, following its pattern: register through the form, open the menu, delete the account, and confirm /login no longer accepts the credentials. In its sandbox the agent runs pnpm install --frozen-lockfile, pnpm check for Biome through ultracite, and the auth spec with pnpm exec playwright test tests/e2e/auth.test.ts under PLAYWRIGHT=True against a Postgres it starts and migrates with pnpm db:migrate, and quotes the results. The description notes one thing a reviewer might ask: votes key on the chat and the message, not on the voter, so a deleted user's votes in someone else's public chat are that chat's rows and need no cross-user cleanup.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add account deletion to this app. Read lib/db/schema.ts, lib/db/migrations/0000_initial.sql, lib/db/queries.ts, app/(auth)/auth.ts, app/(auth)/actions.ts, components/chat/sidebar-user-nav.tsx, components/chat/sign-out-form.tsx and tests/e2e/auth.test.ts first. Add deleteUserById to queries.ts that deletes, inside one transaction and in the order the foreign keys allow, the user's suggestions, documents, votes, messages, streams, chats and then the user; reuse the steps deleteAllChatsByUserId already takes. Add a deleteAccount server action that reads the session, refuses without one or for a guest, calls the query and signs out. Add a Delete account item with a confirmation to the user menu, hidden for guests. Do not add a migration or change the schema. Add a Playwright test in tests/e2e/auth.test.ts that registers, deletes the account and confirms the login fails. Run pnpm check and the auth spec against a migrated Postgres, quote the results, 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 with four files changed: lib/db/queries.ts with deleteUserById, app/(auth)/actions.ts with deleteAccount, components/chat/sidebar-user-nav.tsx with the menu item and confirmation, and tests/e2e/auth.test.ts with one more test. There is no migration, since the schema does not change, and package.json and pnpm-lock.yaml are untouched.

The description lists the deletion order with the foreign key that forces each step, quotes pnpm check and the auth spec's results including the new test, and shows the transaction so a reviewer can see that a failure midway leaves the account whole. The run's record keeps the Postgres start, db:migrate and the Playwright output.

Before merging, a reviewer decides whether a deleted account should also clear files in Vercel Blob that BLOB_READ_WRITE_TOKEN writes, which the schema does not track; the agent leaves them and names the question.

Run it on every Linear issue labelled privacy

Requests like this arrive as tickets. An automation with a linear trigger on linear.issue.labeled, with privacy in config.labels, starts a run for each issue that gains the label, with the issue in the prompt, and opens the pull request against the repository with the issue linked; the agent reads the issue for the retention the team wants, such as a grace period before the rows go, and shapes the action to it.

The trigger adds the issue to the prompt after it. One run per label event, so a label removed and added again starts another, which the automation's max_runs_per_hour bounds.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

Deletion order is a property of the schema, not of the feature request, and getting it wrong shows up as a foreign key error in production. The agent reads every references() and the SQL that created them, writes the steps in the only order Postgres accepts, and runs them in a transaction against a real database before the pull request opens.

The run's record holds the migrated database, the Playwright run and the Biome check, so a reviewer reads what happened rather than a plan. Across a team's apps on this template the same request runs at once, one sandbox each, with each pull request's order derived from its own schema.

Frequently asked questions

Why not ON DELETE CASCADE?

It would need a migration that rewrites seven foreign keys and makes every delete of a user or chat silent about what it removed. The explicit order in deleteUserById reuses what deleteAllChatsByUserId already does and returns counts; the description names cascade as the alternative for a team that prefers it.

What about guest users?

createGuestUser makes a User row with a generated email and isAnonymous set, and the session carries type: "guest". The menu item is hidden for them and the action refuses a guest session, so a guest cannot delete an account they never registered.

Is the delete in a transaction?

Yes. deleteUserById runs its steps inside db.transaction, so a foreign key error on any step rolls the whole thing back and the account remains as it was, which the description shows.

Does this remove files in Blob storage?

No. The schema holds no record of uploaded files, so the agent cannot find them from the database. It names that in the pull request as a decision for the reviewer.

More Drizzle jobs

The same job on other stacks

Keep reading