feat(trpc): erase PostHog and never-charged Stripe customers in the account purge
Summary
admin.deleteUser (the hard purge at the end of account deletion) now also erases the user's data in PostHog and Stripe:
- PostHog: deletes the person whose distinct_id is the user id, along with their events.
- Stripe: deletes the customer of each sole-member org the purge removes, unless that customer was ever successfully charged.
- Sentry is intentionally out of scope. Nothing in this PR touches it.
How it works
PostHog. Every client calls posthog.identify(user.id, …), so the purge sends POST /api/projects/:project/persons/bulk_delete/ with { distinct_ids: [userId], delete_events: true }. It uses the existing POSTHOG_API_KEY / POSTHOG_PROJECT_ID / POSTHOG_API_HOST, with the same bearer-auth pattern as posthog-client.ts and posthog-hogql.ts. The new helper is lib/posthog-persons.ts.
- PostHog returns 202 with
persons_found: 0when no person matches the distinct_id. A missing person therefore counts as success. - A non-2xx response, or a non-empty
deletion_errors, throws. - The person is deleted right away. Events are queued for PostHog's async deletion job.
- This call runs first, before any Stripe call or org delete. If the key is missing the scope, the purge fails before it deletes anything that can't be undone.
Stripe: the paid-customer exception. For a sole-member org, after the existing subscription cancellation, the purge pages through charges.list({ customer }):
- If any charge has
status === "succeeded", the customer and all its billing data are kept. That includes charges later refunded, which keepsucceededstatus. - Otherwise the purge calls
customers.del. - Shared orgs keep their customer and only get the existing seat decrement.
Re-runs. Both external deletions still run before the tombstone transaction. I checked how Stripe behaves in test mode:
- On a deleted customer,
subscriptions.listandcharges.listreturn empty lists. - A second
customers.delfails with 404resource_missing, which the purge treats as already deleted.
So a purge that fails partway can be re-run on the same user id.
The purge still sends no emails. The doc comment on admin.deleteUser now describes all of this, including the keep-paid-customers rule.
⚠️ Before deploying: PostHog key scope
persons/bulk_delete requires a personal API key with person:write. The existing admin dashboard calls only need read scopes.
- The
POSTHOG_API_KEYin the local worktree.envis the personal key "Admin Dashboard Key". It has scopes["*"]and isn't limited to specific projects or organizations, so it already works. - I can't read the production GitHub secret
POSTHOG_API_KEY. If production uses a different key, addperson:writeto it (or swap in a key that has it) before deploying. Without that scope, every purge fails at the first step withPostHog person deletion error: 403, and nothing gets deleted. POSTHOG_API_HOSTisn't set in the deploy workflows, so it defaults tohttps://us.posthog.com, which is correct for US Cloud. No new env vars.
Not included (flagging for a decision)
- Session recordings.
bulk_deletealso acceptsdelete_recordings: true. I left it off because the decision was "person and their events". Say if you want replays erased too; it's a one-line change.
Testing
- New
packages/trpc/src/router/admin/admin.test.tsrunsadmin.deleteUserthrough a real tRPC caller, with fake db, Stripe, andfetch. It covers:- Call order: PostHog, then subscription cancel, charge check, customer delete, org delete, and last the tombstone
- A refunded succeeded charge keeps the customer
- A shared org keeps its customer and gets its seats lowered
- A re-run with the person and customer already gone completes
- A PostHog 403 stops the purge before Stripe or the DB
- A Stripe error other than
resource_missingleaves the user untombstoned
- Mutation-checked: breaking the refunded rule or the
resource_missingcheck each makes a test fail. cloud-guards.test.ts: its@superset/db/clientstub now also listsdbWs. Bun keeps the export names from the first stub of a module for the whole run, so without itadmin.test.tsfails to importdbWswhen both run together.bun testinpackages/trpc: 290 pass, 0 fail.bun run typecheckinpackages/trpc: clean.biome checkon the changed files: clean.- Live API checks (no real user data touched):
- Stripe: probed deleted and missing customers with the test-mode key, using a throwaway customer.
- PostHog: sent
bulk_deletewith a random distinct_id to the dev project (264828), which returned202 {"persons_found":0,…,"deletion_errors":[]}.
- Not run: an end-to-end purge of a real user against production PostHog or live Stripe.
Summary by cubic
Extends the admin account purge (admin.deleteUser) to also erase the user's PostHog person and, for sole-member orgs, delete the Stripe customer unless it was ever successfully charged.
- Deletes the PostHog person (distinct_id = user id) and events first, before any other external call.
- Deletes Stripe customers of sole-member orgs only when no charge ever succeeded; refunded charges still count as paid.
- Shared orgs keep their customer and get only the existing seat decrement.
- Treats already-deleted persons and customers as success, so a purge that fails partway can be re-run.
- Sentry is untouched.
Before deploying
POSTHOG_API_KEYmust have theperson:writescope, or every purge fails with a 403 before anything else happens.
Summary by CodeRabbit
- New Features
- Account deletion now removes the associated PostHog person and event data.
- Stripe customer records are deleted only when no successful charges exist, preserving billing history for customers with paid transactions.
- When deleting the sole member of an organization, active subscriptions are canceled or adjusted and eligible billing records are removed.
- Account deletion can be safely retried without duplicating or disrupting completed cleanup steps.