Dexaloop › Tech › Security and privacy

Security and privacy · How-to

Leaked API key: what to do first, and why deleting the commit is not enough

A leaked key stays usable until the provider stops accepting it, so the fix happens in the provider's dashboard, not in your repository.

By DexaloopPublished 9 November 202610 min readSources listed below

Key takeaways

  • Revoke or rotate the key at the provider first. GitHub's own docs say this is the first step, before any history cleanup.
  • Deleting the file, the commit or even rewriting history does not stop the key working. Clones, forks, cached views and pull requests can still hold it.
  • In Truffle Security's study of 10,000 leaked secrets, 74% were still live 31 days later, and in more than half the cases where the file was taken down, the key still worked.
  • After rotating, check the provider's usage logs for calls you did not make, and look for other secrets in the same file.
  • Some providers now revoke reported keys for you. Resend, for example, disables a key reported by TruffleHog and emails your admins, but you still need a replacement.
On this page
  1. What you need
  2. Why deleting the commit does not fix a leaked key
  3. What to do when you leak an API key, in order
  4. How rotation works on AWS and Stripe
  5. How to remove an API key from Git history
  6. A worked example: a .env file pushed to a public repository
  7. Automatic revocation: what GitHub and providers now do
  8. Troubleshooting
  9. How to stop leaking keys next time

If you have a leaked API key, the first thing to do is revoke or rotate it at the provider: open the dashboard of the service that issued it, create a replacement, and make the old key stop working. Do this before you touch your Git history. Deleting the file or the commit does not stop a key from working, and copies can survive in forks, clones and caches. GitHub's own guide puts it plainly: "as a first step you need to revoke and/or rotate that secret."

This guide covers why deleting is not enough, the order of steps that works for any provider, how rotation looks on AWS and Stripe, how to clean Git history properly, and what the new automatic revocation programs change.

Revoke

The old key stops working. This is what actually ends the risk.

Rotate

A new key replaces the old one, so your app keeps running.

Investigate

Usage logs show whether anyone used the key while it was exposed.

Clean up

History rewrites and secret scanning come after the key is dead.

What you need

  • Access to the dashboard of the service that issued the key, so you can create a new key and make the old one stop working.
  • A way to put the new key where your app reads it: your hosting platform's secret store, CI secrets, or environment variables, and the ability to deploy.
  • The provider's usage logs, to check for calls you did not make.
  • Only for the optional history cleanup: a fresh clone of the repository and git-filter-repo version 2.47 or later.

Why deleting the commit does not fix a leaked key

An API key is a password for a program. The provider checks it on every request. Your repository has nothing to do with that check. So if you delete the file, push a new commit, or even make the repository private, the key is just as valid as it was a minute before. Anyone who copied it can keep using it.

Copies are also hard to chase. GitHub's documentation lists where a secret can still be reached after you rewrite history and force push:

  • In other people's clones: "You cannot remove sensitive data from other users' clones of your repository".
  • In forks: "If the commit that introduced the sensitive data exists in any forks, it will continue to be accessible there."
  • By commit hash, "in cached views on GitHub".
  • "Through any pull requests that reference them".

The data shows how often people skip the real fix. In research run from 28 September to 17 October 2023 and published on 10 January 2024, Truffle Security tracked 10,000 live secrets in 9,399 public GitHub files. It found that "74% of all leaked keys were still live/valid 31 days after being leaked." It also found that "In more than 50% of cases when exposed keys were taken down off GitHub, they remained live/valid": the developer deleted the file but never rotated the key. Its conclusion: "The only secure way to remediate an exposed API key or password is by rotating that secret's value."

What to do when you leak an API key, in order

These steps work for any provider. The names of buttons differ, but the order does not.

  1. Find out which key leaked and who issued it. Look at the file you exposed. A .env or config file often holds several secrets at once, such as a database password next to an email API key. Treat every secret in that file as leaked, not just the one you noticed.
  2. Create a replacement key in the provider's dashboard, ideally with fewer permissions than the old one.
  3. Put the new key where your app reads it: your hosting platform's secret store, CI secrets, or environment variables. Deploy.
  4. Revoke the old key. For a leaked key, do this now, not after a long overlap. If you cannot swap keys quickly, revoke anyway: a short outage is cheaper than a stranger using your account.
  5. Check the logs for misuse. Look at the provider's usage records for the whole time the key was exposed. Unusual volume, regions, models or endpoints are the signs to look for. Keep a copy of what you find.
  6. Find every other copy. Search CI logs, build artifacts, published packages, container images, chat messages and wikis for the same value.
  7. Clean the Git history if you want to. This is optional once the key is dead, but it stops the old value from showing up in scans. See the section below.
  8. Stop it happening again with push protection, scoped keys and a secret store.
Replace andrevoke Check usagelogs Find othercopies Clean Githistory Prevent arepeat Do first Optional History cleanup never comes before the key is dead
The order of response. The key is replaced and revoked at the provider first, and Git history cleanup, which is optional once the key is dead, comes after.

Rotate, revoke and delete compared

ActionWhat it doesDoes it stop the leaked key?
Delete the file or commitRemoves the key from the latest version of your codeNo
Make the repository privateHides it from new visitorsNo, existing copies still work
Rewrite Git history and force pushRemoves the key from your repository's historyNo, and copies can stay in forks and caches
Revoke, disable or expire the keyThe provider rejects the old keyYes
Rotate the keyIssues a new key and retires the old oneYes, once the old key is retired

How rotation works on AWS and Stripe

Providers use different words for the same idea. Two examples from the providers' own docs show the pattern, and one detail that matters when the key has leaked.

AWS IAM access keys

The AWS IAM User Guide describes a no-downtime rotation:

  1. "While the first access key is still active, create a second access key." In the console this is Users, then the user, then the Security credentials tab, then Create access key. From the CLI it is aws iam create-access-key.
  2. "Update all applications and tools to use the new access key."
  3. Check whether the old key is still in use with its Last used information, or aws iam get-access-key-last-used.
  4. Deactivate the old key (Actions, then Deactivate, or aws iam update-access-key to set it Inactive).
  5. Confirm your apps work with only the new key, then delete the old one (aws iam delete-access-key).

AWS suggests one approach is to "wait several days" before moving on from step 3. That advice is for routine rotation. When a key has leaked, deactivate it as soon as your replacement is deployed. Deactivating, unlike deleting, can be undone if something you forgot still needs it.

Stripe secret and restricted keys

Stripe's docs say rotating a key "revokes it and generates a replacement key that's ready to use immediately", and list a compromised key as a reason to do it. On the API keys page, open the overflow menu for the key, select Rotate key, and pick an expiration. Stripe notes that when you rotate in the Dashboard, "both the old and new keys work for up to 7 days" to avoid downtime. For a leaked key, that overlap is the problem: choosing Now means "the old key is deleted." Stripe also lets you open the request logs for any key from the same menu, which is where you check for misuse.

How to remove an API key from Git history

Do this only after the key is revoked. GitHub's guide uses git-filter-repo, and you need "at least version 2.47" for its --sensitive-data-removal flag. Work in a fresh clone.

To remove a whole file from every commit:

git-filter-repo --sensitive-data-removal --invert-paths --path PATH-TO-YOUR-FILE

To replace just the secret's text, list it in a file outside the repository (one value per line, for example EXAMPLE_KEY) and run:

git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt

Then overwrite the repository on GitHub:

git push --force --mirror origin

Know the side effects before you start. GitHub says rewriting history "will change the hashes of the commits" involved. It also says "you will no longer be able to see these diffs" for the pull request that added the secret and for later pull requests built on that history. Everyone with a clone has to clean their own copy. GitHub Support can clear cached views and pull request references, but GitHub says it "will only assist in the removal of sensitive data in cases where we determine that the risk can't be mitigated" by rotating the affected credentials. Forks are separate: the data stays there, and "GitHub cannot provide contact information for these owners." This is why rotation, not history cleanup, is the fix.

A worked example: a .env file pushed to a public repository

Say you push a project to a public GitHub repository and realise an hour later that .env went with it. It holds EMAIL_API_KEY=EXAMPLE_KEY and DATABASE_URL with a password.

  1. In the email provider's dashboard, create a new key with only the permission your app needs.
  2. Change the database password, since it was in the same file.
  3. Put both new values in your host's secret settings and redeploy, then delete the old email key.
  4. Check the email provider's logs and the database's connection logs for the past hour.
  5. Add .env to .gitignore, remove it from the repository, and, if you want a clean history, run the git-filter-repo steps above.

The order matters. If you spent that first hour on step 5, both secrets would have stayed usable the whole time.

Automatic revocation: what GitHub and providers now do

You may not be the first to notice a leak. For public repositories, GitHub secret scanning "runs automatically for free." When it finds a partner's secret format, GitHub says "we notify the provider so they can take action, such as revoking the credential." Those partner secrets "aren't displayed in your repository alerts", so you might only hear about it from the provider.

Separately, Truffle Security launched its Secrets Revocation Program on 4 November 2026, aimed at "the 1.4 million secrets we've confirmed are live and leaked on GitHub, Docker Hub, npm, Hugging Face and more." Truffle says "Sixteen issuers have already joined", and names Hugging Face, Notion, Vercel, Resend, RubyGems and Twilio among its partners. Where the issuer's own team does the revoking, Truffle's terms are: "You revoke within 14 days, or tell us why you can't." Resend's changelog for 4 November 2026 explains how it works for its users: TruffleHog scans public code, packages, container images and datasets, and when it reports a Resend key, "We disable the exposed key as soon as the report arrives" and "We email all admins with a link to where the key was found." Resend says this is on by default, and gives its reason: "TruffleHog found that 81% of the leaked keys it reported last year were still live six months later."

Automatic revocation helps, but it does not replace your own steps. It covers only some providers, it can break your app until you deploy a new key, and it does nothing about the other secrets in the same file.

Troubleshooting

  • You cannot swap keys quickly. Revoke anyway. A short outage is cheaper than a stranger using your account.
  • Your app stopped working after the provider disabled the key. Automatic revocation can break your app until you deploy a new key. Create a replacement, put it in your secret store and redeploy.
  • The old key still works after you rotated on Stripe. When you rotate in the Dashboard, both keys can work for up to 7 days unless you choose Now as the expiration.
  • The key still turns up after you deleted the commit. Deleting does not stop the key working, and copies can stay in clones, forks, cached views and pull requests. Revoke or rotate it: that is the fix. GitHub Support can clear cached views and pull request references, but only "in cases where we determine that the risk can't be mitigated" by rotating the affected credentials.
  • Teammates or forks still hold the old history. Everyone with a clone has to clean their own copy, and GitHub cannot contact fork owners for you. This is why a dead key, not a clean history, is the fix.
  • You are not sure whether anyone used the key. Check the provider's own records: AWS shows when an access key was last used, Stripe lets you open the request logs for any key, and Resend points to its Logs and Emails pages. Look for calls, regions or volumes you do not recognise.

How to stop leaking keys next time

  • Keep push protection on. GitHub's push protection for users "Is enabled by default" and "Stops you from pushing secrets to public repositories on GitHub." It blocks a push before the secret reaches the repository, but only for the secret types GitHub supports, and GitHub says "you can bypass push protection for users", so treat a block as a stop sign, not a formality.
  • Keep keys out of code. Stripe's advice applies everywhere: "Don't put keys in source code or configuration files checked into version control." Use your platform's secret store, or environment variables if there is none.
  • Use narrow keys. Stripe recommends restricted keys over full secret keys, and Resend's leak guide suggests a separate key per service with sending-only access. A narrow key limits what a leak can do.
  • Watch your workflows. CI jobs that run untrusted code with secrets are another common leak path. Our explainer on pull_request_target vs pull_request covers the GitHub Actions version of this risk.

For more on threats and fixes, see the security section of Dexaloop Tech.

Questions

Is deleting the commit enough if I leaked an API key?

No. Deleting the file or commit only changes what your latest code shows. The key still works until the provider revokes it, and copies can remain in clones, forks, cached views on GitHub and pull requests. GitHub Docs say the first step is to revoke and/or rotate the secret.

What is the difference between revoking and rotating an API key?

Revoking (also called deleting, disabling or expiring, depending on the provider) makes the old key stop working. Rotating means issuing a new key and retiring the old one. For a leaked key you want both: a new key for your app, and the old key dead as soon as possible.

Do I still need to clean my Git history after rotating the key?

It is optional for security once the key is dead, but often worth doing so the old value stops triggering scanners and alarms, and so nobody mistakes it for a live secret. GitHub's guide uses git-filter-repo 2.47 or later and a force push, and warns that rewriting history changes commit hashes.

Will GitHub revoke my leaked key automatically?

Not by itself. For public repositories, GitHub secret scanning runs automatically for free, and for partner secrets GitHub notifies the provider so they can take action, such as revoking the credential. Whether the key is revoked depends on the provider. Rotate it yourself either way.

How do I know if someone used my leaked key?

Check the provider's own records. AWS shows when an access key was last used, Stripe lets you open the request logs for any key, and Resend points to its Logs and Emails pages. Look for calls, regions or volumes you do not recognise.

Sources

Written from the sources above and checked against them before publishing. Found a mistake? Email support@dexaloop.com and we will correct it.

More in Security and privacy