Skip to content

Guides · VibeVigil

How to Let Users Delete Their Account in a Lovable or Supabase App (Without Leaking Your Service Role Key)

A "Delete my account" button looks simple, but in a Supabase app it is easy to get wrong: a service role key in the browser, a function that deletes any user ID it is sent, or photos left behind in Storage. Here's how to check yours and build it safely in Lovable, Bolt or Cursor.

Not sure your app is safe? Run a free scan first.
VibeVigil checks your live web app for exposed keys (including a Supabase service role key in your JavaScript), Supabase tables a stranger can read, missing security headers, SSL problems and downtime. Every issue comes with a ready-to-paste prompt for your coding agent.
Scan my app for free → (only scan apps you own)

Note: the scan does not press your delete button or call your Edge Functions. Use the steps below to test those yourself.

How to Let Users Delete Their Account in a Lovable or Supabase App

Sooner or later a user asks: "How do I delete my account?" Maybe they want to leave. Maybe the law where they live gives them the right to ask. And if you ship an iPhone app that lets people sign up, Apple asks that they can also start deleting their account inside the app (Apple App Review Guidelines 5.1.1(v)).

So you ask your coding agent to "add a delete account button." It works in the demo. But this one button touches the most powerful key in your project, and it is easy to build in a way that hurts you.

This guide shows the three common mistakes, how to check for each one, and a safe way to build it. Only test projects you own.

The short answer

  1. Deleting a Supabase user needs the service role key. Supabase says auth.admin.deleteUser() "requires a service_role key" and "should only be called on a server. Never expose your service_role key in the browser" (Supabase: deleteUser).
  2. So the delete must run on the server, for example in a Supabase Edge Function. Never in your React code.
  3. The function must delete the person who is calling it, read from their login token. It must never delete "whatever user ID the request sends."
  4. Delete their files too. Supabase says you "cannot delete a user if they are the owner of any objects in Supabase Storage" (Supabase: User Management). Remove files with the Storage API, not with SQL.
  5. Set up your tables so their rows go with them (on delete cascade), or delete those rows in the same function.

Mistake 1: The service role key ends up in the browser

This is the big one. The agent tries supabase.auth.admin.deleteUser() in the front end. It fails, because the public (anon or publishable) key can't do that. To "fix" the error, the agent swaps in the service role key.

Now the button works. But the service role key skips Row Level Security (RLS). Anyone who opens your site's JavaScript can copy it and read, change or delete every row and every user in your project.

How to check (2 minutes):

  • Search your code for service_role, SERVICE_ROLE, auth.admin and deleteUser. Any hit in front-end files (src/, components, pages) is a problem.
  • Check your env variables. Anything that starts with VITE_, NEXT_PUBLIC_ or EXPO_PUBLIC_ is sent to the browser. A service role key must never have one of those names.
  • Open your live site, press F12, go to Sources, and search the JavaScript for service_role. You can also run the free VibeVigil scan, which looks for secret keys in your public JavaScript.

If you find it: move the code to the server (see the fix below), then rotate the key in the Supabase dashboard. Moving it is not enough. Assume someone already copied it. See How to Rotate a Leaked API Key and Is a Supabase service role key safe in the browser?

Mistake 2: The function deletes any ID it is sent

The agent moves the delete into an Edge Function. Good. But the code looks like this:

// ❌ Dangerous: deletes whatever ID the caller sends
const { userId } = await req.json()
await admin.auth.admin.deleteUser(userId)

Anyone who is logged in (or anyone at all, if the function is open) can send someone else's user ID and delete their account. User IDs often show up in URLs, API responses and the network tab, so they are not secret.

How to check (5 minutes):

  1. Make three test accounts: A, B and C.
  2. Log in as B. Open the network tab (F12) and press "Delete account." Look at the request it sent.
  3. If the request contains a user ID, log in as C. Send the same request again (with curl or your browser's "Copy as fetch"), but put account A's ID in it.
  4. If account A disappears, the function trusts the request. Fix it.

Also call the function with no login at all. If it does anything other than say "not logged in" (401), see Can Anyone Call My Supabase Edge Function?

Mistake 3: Their photos and files stay behind

Your app may store avatars, selfies, PDFs or receipts in Supabase Storage. Two things go wrong here:

  • The delete fails. Supabase won't delete a user who still owns Storage files. You get an error and the account stays.
  • Files get orphaned. Supabase warns that deleting objects "via a SQL query will not remove the object from the bucket and will result in the object being orphaned" (Supabase: Delete Objects). The row is gone, but the file is still there.

If your app promises "we delete your photos," test it: upload a file, copy its URL, delete the account, then open the URL again. If the file still loads, it wasn't deleted. If the bucket is public, anyone with that link can still see it. See Is My Supabase Storage Bucket Public?

The safe way to build it

1. Put the delete in an Edge Function. Keep verify_jwt on (the default).

2. Get the user from their login token, not from the request body.

// ✅ Deletes only the person who is calling
const authHeader = req.headers.get('Authorization') ?? ''
const userClient = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
  global: { headers: { Authorization: authHeader } },
})
const { data: { user } } = await userClient.auth.getUser()
if (!user) return new Response('Not logged in', { status: 401 })

const admin = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY) // server-side secret only

3. Delete their files with the Storage API. If you save files in a folder named after the user ID (like avatars/USER_ID/photo.png), list that folder and remove the files with admin.storage.from('avatars').remove([...]). Note: remove handles up to 1,000 files per call.

4. Delete or clean their rows. The easy way is to link your tables to auth.users with on delete cascade, as Supabase shows (Supabase: User Management). Then rows like profiles go away when the user does. Check tables that hold their data but don't link to auth.users.

5. Delete the user. Call admin.auth.admin.deleteUser(user.id). Supabase says this removes the user's sessions so they can't get new login tokens.

6. Sign them out in the app. Supabase notes that a login token already issued stays valid until it expires (Supabase: User Management). Call supabase.auth.signOut() on the device and send them to the home page.

7. Don't forget other services. If they pay through Stripe, cancel the subscription first, so they aren't charged after they leave. If you copy emails into a mailing tool, remove them there too.

8. Ask "are you sure?" A delete can't be undone. A simple "type DELETE to confirm" stops accidents.

Prompt for your coding agent

Copy this into Lovable, Bolt, Cursor or Claude Code:

Please add (or fix) a safe "Delete my account" feature for this Supabase app.

1. Search the whole project for service_role, SERVICE_ROLE, auth.admin and deleteUser. If any of these are in front-end code or in env variables starting with VITE_, NEXT_PUBLIC_ or EXPO_PUBLIC_, remove them from the front end and tell me so I can rotate the service role key.
2. Create a Supabase Edge Function called delete-account with verify_jwt on.
3. In the function, get the user from the Authorization header with supabase.auth.getUser(). Return 401 if there is no user. Never read a user ID from the request body.
4. With a service role client (key from server-side secrets only): cancel any active Stripe subscription for this user if the app uses Stripe; delete the user's files in every Storage bucket using the Storage API remove() method (not SQL); delete or cascade their rows in all tables that store their data.
5. Then call auth.admin.deleteUser(user.id).
6. In the app, add a "Delete my account" button in settings with a confirm step (type DELETE). After success, call supabase.auth.signOut() and go to the home page.
7. Show me which tables reference auth.users and whether each one uses on delete cascade. List any table that holds user data but is not cleaned up.
8. Give me curl commands to test: no login (should be 401), and logged in as user B trying to delete user A (should only ever delete B).

Do not turn off Row Level Security and do not put the service role key in the browser.

Quick checklist

  • [ ] No service role key or auth.admin call anywhere in front-end code or public env variables
  • [ ] The delete runs in an Edge Function (or other server code)
  • [ ] The function gets the user from their login token, never from the request body
  • [ ] Calling the function with no login returns 401
  • [ ] User B can't delete user A
  • [ ] Their Storage files are removed with the Storage API, and the old file URL stops working
  • [ ] Their rows are deleted or cascade with auth.users
  • [ ] Paid plans are canceled before the account is deleted
  • [ ] The app signs them out after deleting

Related guides


Want a check on the public side of your app? VibeVigil looks for exposed keys, open Supabase tables, headers, SSL and uptime, and gives you a fix prompt for each issue. Run a free scan → (only scan apps you own)

Scan your app free. It checks the public page of a site you own. No account.

How to Let Users Delete Their Account in a Lovable or Supabase App (Without Leaking Your Service Role Key) · VibeVigil