How-to guide

How to clean up old service accounts and API keys

Learn to inventory non-human identities, find unused service accounts and API keys, cut their permissions, and retire them without breaking anything.

By the Cloudverg engineering team · · 6 min read

Why this matters

Service accounts and API keys are identities with no person behind them. Nothing about them expires on its own. Nobody gets a reminder when the contractor who created one stops showing up. They sit in the console with whatever permissions they had on the day they were made, and they keep working.

That is the problem. A key created for a two-week migration is still valid two years later. A service account that started read-only had admin added to fix one bug and never lost it. When a key leaks through a commit, a build log or a screenshot in a ticket, the attacker gets a working credential that nobody is watching. Cleaning up shrinks that exposure and turns "who can read this bucket" into a short list.

There is an operational payoff too. Customer security reviews ask for a list of non-human identities with owners, permissions and rotation dates. If you can produce that in an hour, the review moves. If you have to guess, it becomes a project with a deadline.

Before you start

Pick a scope you can finish. One cloud account, or one product's services, beats everything at once. Write down the boundary, because the hard part is deciding what is out of scope and being honest about it.

You need read access to authentication logs. Last-used data is the core of this job. On AWS that means CloudTrail and the IAM credential report. On Google Cloud it means audit logs and the service account usage metrics. On Azure it means Entra ID sign-in logs for app registrations and service principals. Pick a lookback window, 90 days is common, and use the same one for the whole pass so the numbers mean something.

Build the inventory from more than the console. Keys live in CI/CD secret stores, .env files on servers, Kubernetes secrets, Terraform state, SaaS admin panels and somebody's notes. Ask each team for their list before you start. People remember what they own when you ask them directly.

Set a grace period and a communication plan before you disable anything. Two weeks minimum, a message to the owning team, a ticket, a calendar date. Then keep the date.

How to do it

How to clean up old service accounts and API keys comes down to one habit: a pass you repeat, not a purge you do once. Work in one direction: inventory, ownership, permission cuts, rotate, disable, delete. Other orders cause an outage at 2 AM.

The steps below assume one cloud account and a 90 day lookback. Repeat for the next account once the pattern is boring.

Keep one spreadsheet or ticket board as the source of truth. Every identity gets a row: name, provider, owner, last used, current permissions, decision. That row is what you show in a review.

  1. Export every non-human identity. Pull service accounts, app registrations, service principals, access keys and API tokens from each provider and from your identity provider. Add the entries you find in CI/CD secret stores and password managers. Keep the owner column even when it is blank, because the blanks are the point.
  2. Assign an owner and a purpose. Send each row to the team that seems to own it and ask for a person plus one sentence on what it does. If nobody claims a row within two weeks, it goes on the removal list. Do not accept a team name as an owner.
  3. Find what has not been used. Sort by last authentication time and flag anything with no activity in your lookback window. Check both the credential's last use and the service account's last activity, since a key can be idle while the account is busy. Read the API call logs, not just console sign-ins.
  4. Cut permissions before you delete. Compare each surviving account's role to the calls its logs actually show. Drop admin, wildcard actions and resource scopes it never touches. Access analyzers in each cloud can suggest a policy from observed activity, but read the suggestion before you apply it, because it may still be wider than the job needs.
  5. Issue a replacement with a narrower scope. Create the new key or service account with only the permissions the old one needed, which is often less than it had. Deploy it to every consumer and confirm the traffic. Keep both credentials alive during the switch.
  6. Disable instead of deleting. Set the old credential to inactive and leave it there for 14 to 30 days. Watch the logs for authentication failures from anything still using it. This window is where you find the cron job nobody mentioned.
  7. Delete and record the date. After the window passes with no failures, delete the credential and remove it from every secret store and config repo. Close the ticket with the date and the owner. A deleted key with a copy in a repository is not deleted.
  8. Fix the intake. Every new key request should carry an owner, a purpose and an expiry date. Anything missing one of the three gets rejected. This step is what keeps the next cleanup small.

How to check it worked

Re-run the inventory query you started with. The list should be shorter, and every remaining row should have a person and an expiry date. A row without an owner means the pass is not finished.

Search the logs for authentication failures against the identities you disabled. Real traffic means something still depends on them, and you found it during the grace window instead of after the delete. That is the whole reason for disabling first.

Grep the code, config repos and Terraform state for the deleted names and for key prefixes. Old keys hide in Docker image layers, test fixtures, vendor dashboards and build scripts. Deleting in the console does nothing about a copy in git history.

Check that no new credentials appeared without an owner during the cleanup. The intake rule matters more than the cleanup itself.

Mistakes to avoid

Deleting on the spot. The key that looks unused might run a monthly billing export. Disable it, wait, then delete.

Trusting the console as the whole picture. The vendor integration, the cron job on a VM and the desktop script someone wrote two years ago are all yours to track. A console inventory covers the identities the console knows about, not the places the keys ended up.

Rotating without listing consumers, then giving the new key the old role. A shared key with six consumers will break something if you rotate it blind. A replacement with the same broad permissions moves the risk instead of removing it.

Closing the ticket without a date and an owner, which means the next pass starts from zero. Put the pass on the calendar: quarterly for keys, and after every departure. Some teams hand the first one to an outside reviewer to get a clean baseline. The Cloud security review from Cloudverg is $6,500/month, a fixed scope read-only review of up to five cloud accounts that covers unused and over-broad permissions with a prioritized fix list. It is one option among several, and it works best when you can name an owner for each finding.

Frequently asked questions

How do I find unused service accounts?

Look at last authentication time, not creation date. Cloud providers keep that data in audit logs and credential reports, and most offer a built-in view of unused access. Use the same lookback window for everything, and treat missing data as unused only after you confirm logging covered that account.

Should I delete or disable an API key first?

Disable it and wait two to four weeks. If something breaks, you can turn it back on in a minute and you have learned about a consumer you missed. Deleting first turns that same discovery into an outage.

How often should API keys be rotated?

Keys that can read data or change infrastructure benefit from a 90 day rotation, and every key should be rotated when the person who created it leaves. Read-only keys tied to a single service can live longer if they have an owner and a documented purpose. The owner matters more than the calendar.

Monthly plan

Want help with this? Cloud security review

$6,500/month

2 to 3 weeks · Starts within 10 business days

A read-only review of identity, network exposure, encryption and logging across your cloud accounts, with a prioritized fix list.

Renews monthly. Cancel any time.
What is included

Related guides

More cloud security guides   Security and compliance

Smallest way to start

Get a senior architect on your problem this week

Ninety minutes a month on the cloud, AI or security question that is blocking you, a pre-read of your material and a written summary within two business days.

$450/month

Renews monthly. Cancel any time.