# Leaked an API key? Do these 5 steps in this order

> Deleting the key from your code is not step 1. Anyone who already copied it can still use it.

Source: https://fullbytedeveloper.com/blog/rotate-first
Published here: 2026-11-12
Tested: 2026-11-08, temp repo, fake key

Deleting a leaked key from your code does not stop anyone who already copied it. The old commits keep it anyway. Five steps in this order, built on GitHub's docs (checked Nov 8): rotate first, rewrite history last.

## 1. Revoke or rotate the key at the provider

This is the only step that stops use. GitHub's docs say "as a first step you need to revoke and/or rotate that secret." [1]

## 2. Check the provider's usage log

Look for calls you did not make, from the first push until you rotated [2].

## 3. Put the new key in an env var or secret store

Never in code.

```javascript
const key = process.env.API_KEY;
```

## 4. Find every copy in history

Search only the first few characters so the full key stays out of your shell history. The diff still prints it.

```bash
git log --all -p --oneline -S "sk_fake_ab"
```

In my test: grep said gone, `git log -S` printed the key from both commits.

## 5. Rewrite history with git-filter-repo

Use version 2.47 or later [1]:

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

The file `passwords.txt` holds the leaked key, one per line. Filter-repo replaces it with `***REMOVED***`. Then force push as the GitHub docs show.

Forks and clones keep the old commits anyway. That is why step 1 comes first.

## The rule

Rotate, check, replace, find, clean.

Send this to the friend who deleted a leaked API key from their code and thinks it is fixed.

## Sources

1. [GitHub docs: removing sensitive data from a repository](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository)
2. [GitHub docs: remediating a leaked secret](https://docs.github.com/en/code-security/secret-scanning/working-with-secret-scanning-and-push-protection/remediating-a-leaked-secret)
3. [Git docs: git-log](https://git-scm.com/docs/git-log)
