Short answer: treat it as an incident, not a cleanup. The service_role key (and the newer sb_secret_… keys) bypasses Row Level Security, so whoever has it can read, change or delete everything. Fix the cause of the leak, create a new secret key, move your server code to it, retire the leaked key in your project's API key settings, then check your data for changes. Supabase's guide: Rotate a leaked API key.
First, confirm what leaked
Not every Supabase key in your app is a problem. The anon / publishable key is meant to be public. Check which one you found:
- Starts with
sb_secret_: a secret key. Leaked. - Starts with
sb_publishable_: public by design. Make sure RLS is on (how to check), but this isn't the emergency. - A long token starting
eyJ: decode the middle section (between the two dots) locally, for example withatob('…')in your browser console, and readrole.anonis fine;service_roleis leaked.
Also note where it leaked: your JavaScript bundle, a VITE_ or NEXT_PUBLIC_ variable, a public GitHub repo, a .env file on your site, or a screenshot or chat. That decides step 1.
The response plan
1. Fix the cause of the leak
Supabase's own guide says to do this first, otherwise the new key leaks the same way. Usually that means the frontend is calling Supabase with the secret key. Move that work into a Supabase Edge Function or server route that reads the key from its secrets and checks the caller is signed in and allowed:
Remove the Supabase service_role / secret key from all frontend code and from any VITE_ or NEXT_PUBLIC_ variable. The browser must use only the Supabase URL and the publishable (anon) key. Move every operation that needed the secret key into a Supabase Edge Function that reads it from Supabase secrets and verifies the signed-in user before doing anything. Show me each file you changed.
If you see signs someone is actively using the key, don't wait for this. Retire it now (step 3) and accept that some features break until you finish.
2. Create a new secret key
In the Supabase dashboard's API Keys settings, create a new secret key. Put it only where server code reads it: Edge Function secrets or your host's server-side environment variables. Update every Edge Function, webhook and backend job that used the old key, and confirm each one works.
3. Retire the leaked key
- New-style
sb_secret_key: delete it in the API Keys settings. Deletion can't be undone, so make sure everything uses the new key first. - Legacy
service_roleJWT: first switch your frontend from the legacy anon key to a publishable key, because deactivating the legacy keys turns off both. Then deactivate the legacy keys, and revoke the legacy JWT secret by moving to JWT signing keys, as Supabase's guide describes.
4. Check for damage
Rotation stops future access. It doesn't tell you what already happened. Over the period the key was exposed:
- Review your project's API logs for unusual volume, unfamiliar sources, or bulk reads and deletes.
- Spot-check important tables for rows that changed, disappeared or appeared.
- Look through your project's user list for accounts you don't recognize, and at Storage for unexpected files.
- If data was changed or deleted, restore from a backup (availability depends on your plan).
If personal data may have been read, check whether you're required to tell your users. That depends on where you and they are.
5. Clean up where it leaked
Redeploy the app so the old bundle is gone, remove the key from your repo, and rotate anything else that sat next to it, such as other keys in the same .env. Cleaning Git history is optional; the rotation is what protects you. If an env file was public, follow Did my .env leak?
Confirm it's gone
Open your live site, search all sources in DevTools for sb_secret_ and service_role, and decode any eyJ token you find. CapLeak's free score runs the same kind of check from the outside. It reads your homepage and the JavaScript files it loads, flags any sb_secret_ key and any Supabase token whose role is service_role (plus other providers' secret keys), and returns a score, a grade and problem counts. The $12 full report (one time, per app) shows where each issue is, with a plain-English fix and a copy-paste prompt for Lovable, Bolt or Cursor, right after checkout. The deep part (database tables, hidden files like .env and .git, and source maps) is added after a quick ownership check, which takes about a minute. It includes 1 re-scan within 14 days, so you can confirm the fix. To keep an app watched every week, Watch is $15 a month per app and Unlimited is $39 a month for up to 25 apps, with no report needed first.
CapLeak runs passive checks only. A passing grade means none of its checks failed; it is not a guarantee of security.