Leaked an API key? What to do in the first 15 minutes
A step-by-step response for a leaked API key or token: revoke it at the provider first, check usage, then remove it from git history with git filter-repo or BFG.
6 min read

Short answer
Revoke or rotate the key at the provider first, before you touch git. Bots have used leaked AWS keys within seconds. Then check the provider's usage and billing, replace the key everywhere it ran, remove it from git history with git filter-repo, ask GitHub Support to purge cached views, and add a pre-commit scanner.
You pushed a commit and saw it a second too late: an API key, in plain text, in a public repository. Or a scanner flagged one in a repo that has been public for months.
Revoke the key at the provider first. Everything else comes after. Deleting the commit, force-pushing or making the repo private does not make the key safe, and the people looking for leaked keys are faster than you are.
How fast is a leaked API key used?
Faster than most people expect:
- In 2023, Palo Alto Networks' Unit 42 watched attackers find and use exposed AWS credentials on GitHub within five minutes.
- Orca Security's 2023 honeypot report saw a leaked key used within two minutes of being pushed to GitHub.
- A 2026 Stony Brook University study left 100 AWS keys in public container images. The fastest was used in 7.86 seconds, the median in 51 seconds, and all 100 within 15 minutes. OpenAI and Anthropic keys in the same study were used to drain account balances.
So assume the key is already in someone else's hands, and work in this order.
Step 1: Revoke or rotate the key
Go to the provider, not to git. Revoking makes the leaked copy useless wherever it ended up: in forks, in clones, in a scraper's database.
| Provider | Where to revoke | Notes |
|---|---|---|
| OpenAI | API keys page, trash icon on the key | OpenAI says it disables keys it finds on the public internet, but do not wait for that |
| Anthropic | Console, Settings, API keys, the key's menu, Delete API key | Anthropic deactivates keys GitHub reports to it and emails you |
| GitHub token | Settings, Developer settings, Personal access tokens, Delete | GitHub revokes tokens pushed to public repos or gists automatically |
| AWS access key | IAM, the user's Security credentials, deactivate then delete | Create the replacement first if production depends on it |
| Google Cloud API key | Credentials, the key, Rotate key, then delete the old key | Deleted keys can be restored for 30 days |
| Stripe | Developers, API keys, the key's menu, Rotate key | Choose to expire the old key now, not in 7 days |
If production uses the key, create the new key first, deploy it, then revoke the old one. If you cannot do that quickly, revoke anyway and accept a few minutes of downtime. A short outage costs less than a drained account.
Will GitHub or the provider revoke it for me?
Sometimes. GitHub's secret scanning partner program tells providers such as OpenAI, Anthropic, AWS, Google and Stripe when their keys appear in public repositories, and many of them revoke the key. GitHub revokes its own tokens.
But GitHub documents no timing guarantee, it only covers public repositories and supported patterns, and it does nothing for a key that leaked somewhere other than GitHub. Revoke it yourself.
Step 2: Check what the key was used for
Open the provider's usage, billing and audit pages and look at the window between the leak and the revocation:
- AI APIs (OpenAI, Anthropic): usage by key and by day. Unexpected spikes usually mean someone ran requests on your balance.
- AWS: CloudTrail events for that access key ID, and
aws iam get-access-key-last-used. Look for new IAM users, new keys and EC2 instances in regions you never use. - Stripe, GitHub, others: the account's security or audit log.
If you see activity you did not cause, contact the provider's support with the key's ID and the time window, and keep screenshots of the usage graphs.
Step 3: Replace the key everywhere it ran
A revoked key breaks everything that used it. Find every place the old key lived:
- deployed apps and their environment variables
- CI secrets
- teammates'
.envfiles - local copies in old projects, backups and notes
The last one is easy to miss. In my case, one key had been copied into 138 files on my own laptop. If you do not know where all your copies are, a scan of your machine is worth the minute it takes.
Step 4: Remove the secret from git history
With the key revoked, cleaning git history is no longer urgent. It is still worth doing, so the dead key stops showing up in every scan, and so nobody mistakes it for live. GitHub's own docs note that rotating may make a history rewrite unnecessary.
Does deleting the commit remove the secret?
No. Adding a new commit that deletes the line leaves the key in history. Force-pushing a rewritten history removes it from your branches, but GitHub keeps cached views and pull request refs that can still be reached by commit SHA, and forks keep their own copy. Truffle Security showed in 2024 that data from deleted forks and deleted repositories can still be read on GitHub.
Is making the repository private enough?
No. When you make a public repository private, its public forks stay public. And the key may already have been copied.
How to remove it with git filter-repo
GitHub recommends git filter-repo, version 2.47 or later, which has a --sensitive-data-removal mode. Work on a fresh clone:
brew install git-filter-repo
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
cd YOUR-REPOSITORYTo remove a whole file from history:
git-filter-repo --sensitive-data-removal --invert-paths --path PATH-TO-YOUR-FILETo replace the secret's text everywhere instead, put the secret on a line in a file outside the repo, for example ../passwords.txt, then:
git-filter-repo --sensitive-data-removal --replace-text ../passwords.txtThen force-push every branch and tag:
git push --force --mirror originYou may need to turn off branch protection for the push. Expect commit hashes to change and commit signatures to be dropped. There is no undo once you have pushed.
Then ask GitHub Support to purge cached views
The force push does not clear GitHub's cached views or the refs behind pull requests. GitHub's docs ask you to contact GitHub Support with the repository, the number of affected pull requests, and the first changed commits. filter-repo prints those for you. Collaborators also need to re-clone; if anyone pushes from an old clone, the secret comes back.
Or use BFG Repo-Cleaner
BFG Repo-Cleaner is older and still popular. It needs Java 11 or newer, and it works on a mirror clone:
git clone --mirror git://example.com/some-big-repo.git
bfg --replace-text passwords.txt some-big-repo.git
cd some-big-repo.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git pushOne trap: by default BFG does not touch your latest commit. Commit the fix that removes the secret first, then run BFG.
Step 5: Make the next leak less likely
Each of these closes a different gap:
- A pre-commit scanner. Gitleaks, Betterleaks or TruffleHog will stop most keys before they reach a remote. Gitleaks vs TruffleHog compares them.
- GitHub push protection. It is on by default for your own pushes to public repositories; for private repositories it needs GitHub Secret Protection.
- Scoped, expiring keys. Give each project its own key with the smallest permissions and a spending limit where the provider offers one.
- No plaintext keys in files at all. A key that is not in a file cannot be committed, copied into a backup, or read by a coding agent that opens your
.env.
That last point is the one most teams skip, and it is getting worse. GitGuardian's State of Secrets Sprawl 2026 counted 28.65 million new hardcoded secrets in public GitHub commits in 2025, up 34%, and secrets for AI services grew 81%. The same report found that more than 64% of credentials that leaked in 2022 were still valid in January 2026.
KeyJar is my answer to it. Your .env holds a reference such as kj://openai/main instead of the key, which is safe to commit. The key lives in one encrypted vault on your laptop, and kj run resolves the references when your command starts. With Pro, Claude Code asks you before it gets a key through kj. The scanner and core CLI are free.
Leaked API key checklist
- Revoke or rotate the key at the provider.
- Check usage, billing and audit logs for the leak window.
- Deploy the new key everywhere the old one ran, including local copies.
- Remove the secret from history with git filter-repo or BFG, and force-push.
- Ask GitHub Support to purge cached views and pull request refs.
- Add a pre-commit scanner and keep push protection on.
Keshav Melnad
Solo founder of KeyJar, building from Egypt. The scanner started as a check of one laptop, which turned up a single Anthropic key copied into 138 files.

