48h onlyLifetime deal: pay once from $100, keep Keysquid forever. No subscription.Ends in 32:06:25See the deal
Skip to content
Rotate › Google Cloud

How to rotate a Google Cloud service account key

A service account can hold up to 10 keys, so you can run old and new side by side. Google's own process is five steps: find the keys, create replacements, deploy them, disable the old ones and watch, then delete. Two details catch people: the console can't disable a key, and deleting a key doesn't kill the access tokens it already minted.

Keysquid team · Updated · Checked against Google Cloud's IAM documentation on December 1, 2026

How to rotate Google Cloud service account key keys with Keysquid

Automatic Keysquid creates, verifies, distributes and revokes Google Cloud service account key keys on a schedule, with no one in the loop. JSON keys for a GCP service account, created and deleted through the IAM API (up to 10 per account).

Setup, as the app shows it:

  1. Create a service account “keysquid-rotator” in any project. Create one JSON key for it and paste it here.
  2. On each service account Keysquid should rotate (not the project), grant keysquid-rotator the role Service Account Key Admin (roles/iam.serviceAccountKeyAdmin).
  3. If your org enforces iam.disableServiceAccountKeyCreation, Keysquid can't create keys; consider Workload Identity Federation instead.
  • Writes to: Key file (JSON) (default variable GOOGLE_APPLICATION_CREDENTIALS_JSON), in every platform and secret store you map it to.
  • Interval: from every 1 hour to every 4 weeks, subject to your plan’s minimum.
  • Inventory: Keysquid lists every Google Cloud service account key key the connection can see, including keys it doesn’t manage, with names and last use.

Every rotation runs in the same order: create the new key, verify it, write it everywhere, redeploy where a platform needs it, and only then revoke the previous key after a grace period. If any write fails, everything written so far is restored and the new key is revoked. The value is encrypted to the agent you run, so Keysquid’s servers never see it.

Under the hood: Google Cloud service account key keys in detail

Everything below is what Keysquid handles for you on every rotation. It’s here so you know exactly what happens to your keys, and in case you ever need to do a step by hand.

Format
A JSON file with "type": "service_account", a private_key_id and a PEM private_key. Google keeps only the public half.
Where
IAM & Admin → Service accounts → the account → Keys tab → Add key → Create new key → JSON.
Shown once
The key file downloads once. You can't download it again.
Limit
Up to 10 keys per service account.
Expiry
None by default. The org policy iam.serviceAccountKeyExpiryHours sets one for new keys.
Disable
Reversible, but only with gcloud or the REST API: the console can show disabled keys, not disable them.
Delete
Permanent. A deleted key can't be restored.
Propagation
After creating a key, Google says to allow 60 seconds or more before using it.
Role needed
Service Account Key Admin (roles/iam.serviceAccountKeyAdmin) on the project or the service account.
Keysquid
Automatic: a new JSON key through the IAM API, delivered to your platforms, the old key deleted.

Before you rotate: do you need a key at all?

Google’s own guidance is to avoid user-managed keys wherever the workload already has an identity. If the code runs on Google Cloud, attach the service account to the resource. If it runs in GitHub Actions, another cloud or Kubernetes, Workload Identity Federation swaps the key for short-lived tokens. A key you don’t have can’t leak, and that beats any rotation schedule, ours included.

Keys are still the practical answer for a lot of third-party tools and hosting platforms that only accept a JSON file. For those, rotate. Google recommends at least every 90 days, and immediately if you suspect exposure.

Rotate a key in the Google Cloud console

  1. Open IAM & Admin → Service accounts, select the project and click the service account’s email.
  2. On the Keys tab, note the ID and created date of the key in use, then click Add key → Create new key, choose JSON and click Create. The file downloads straight away.
  3. Put the file’s contents in your secret store or platform variable, delete the downloaded copy, and redeploy. Wait at least a minute before you conclude the new key doesn’t work.
  4. Disable the old key with gcloud (next section), because the console can’t. Watch your app and logs for a day or so.
  5. When nothing has broken, go back to the Keys tab and click Delete on the old key.

Rotate a key with gcloud

SA=app@my-project.iam.gserviceaccount.com

# 1. See the keys you have, with created and expiry dates
gcloud iam service-accounts keys list --iam-account=$SA

# 2. Create the replacement, writing the file where your deploy step expects it
gcloud iam service-accounts keys create ./new-key.json --iam-account=$SA

# 3. Deploy new-key.json, confirm the app authenticates, then disable the old key
gcloud iam service-accounts keys disable OLD_KEY_ID --iam-account=$SA --project=my-project

# 4. Something broke? Re-enable it. Nothing broke? Delete it for good.
gcloud iam service-accounts keys enable OLD_KEY_ID --iam-account=$SA --project=my-project
gcloud iam service-accounts keys delete OLD_KEY_ID --iam-account=$SA

The key ID is the 40-character hex string in the list output, and the same value as private_key_id inside the JSON file. To find out which key a running service holds, read that field from the file it was deployed with.

Why disable before you delete

ActionReversible?What it stops
DisableYes, re-enable at any timeNew authentication with the key
DeleteNoNew authentication with the key, permanently
Disable the service accountYes, re-enable itEverything that uses the account, including tokens already issued

Disabling gives you an undo button. A forgotten cron job or a second region still on the old key fails loudly, you re-enable the key, fix the deploy and try again. Google recommends exactly this: disable, wait until you’re sure the key isn’t needed, then delete.

The gap in both: neither disabling nor deleting a key revokes short-lived credentials already issued from it. Access tokens are valid for an hour by default. After a leak, if that hour matters, the only way to cut those tokens off is to disable or delete the service account itself, which also stops every workload using it.

The 10-key limit

Every rotation that skips the final delete leaves a live key behind, and at 10 keys the account can’t get another. Before deleting stale keys, check which ones are still used: the Key Authentication Events metric shows when each key last authenticated, and service account insights flag accounts unused for 90 days. Audit log entries from many services include a serviceAccountKeyName field that names the exact key behind a call. For an org-wide sweep, Cloud Asset Inventory can list every key older than a date:

gcloud asset search-all-resources --scope="organizations/ORG_ID" \
  --asset-types="iam.googleapis.com/ServiceAccountKey" \
  --query="createTime < 2026-09-01" --order-by="createTime"

Org policies that change the rules

ConstraintEffectWatch out for
iam.disableServiceAccountKeyCreationNo new user-managed keysEnforced by default for organizations created on or after May 3, 2024. Rotation fails at the create step.
iam.serviceAccountKeyExpiryHoursNew keys expire after a set number of hoursExisting keys keep no expiry. Expired keys fail gradually, over up to several days.
Service Account Key Exposure ResponseSet to DISABLE_KEY and Google disables leaked keys it detectsGoogle doesn't guarantee it finds every leak. Disabled keys carry an exposure reason in their metadata.

If key creation is blocked and you really need a key, an administrator with the Organization Policy Administrator role can exempt one project with a tag rather than switching the policy off everywhere. Changing org policies needs orgpolicy.policy.set, which neither Owner nor Editor includes.

If a key leaked

  1. Disable the key now with gcloud, then delete it once you’ve replaced it. Removing the file from a repository is not enough; the key must be deleted in IAM.
  2. If the service account had broad roles, consider disabling the account itself for an hour to kill tokens already minted from the key.
  3. Search Cloud Audit Logs for the account’s principalEmail and the leaked serviceAccountKeyName to see what was done with it.
  4. Turn on the exposure response policy with DISABLE_KEY so the next leak Google spots is shut off automatically.
  5. Work through the first 10 minutes after a key leak, and if the file was pushed to GitHub, scan the repository’s history with the GitHub secret scanner.

By hand vs. with Keysquid

By hand

  1. Create a key and handle the downloaded file.
  2. Paste the JSON into every platform that uses it.
  3. Disable the old key with gcloud, wait, then delete it.
  4. Keep each account under 10 keys.

With Keysquid

  1. Keysquid creates the key through the IAM API; no file touches a laptop.
  2. It writes the JSON to every platform you mapped and redeploys.
  3. It deletes the old key after the grace period.
  4. It lists every user-managed key on the account, with created dates and status.

What Keysquid does on every rotation

Keysquid writes the key’s JSON content, not a file path. Libraries that read GOOGLE_APPLICATION_CREDENTIALS expect a path, so either load the JSON from the variable in code or have your start command write it to a file first. The rotator service account Keysquid uses is itself a key; keep it in its own project with only the Service Account Key Admin role on the accounts it rotates. If you can move a workload to Workload Identity Federation instead, do that and take it off the rotation list.

Frequently asked questions

How many keys can a Google Cloud service account have?

Up to 10. Old keys left behind by past rotations count towards the limit, so delete them once you're sure they're unused.

Can I disable a service account key in the console?

No. The console shows disabled keys but can't disable one. Use gcloud iam service-accounts keys disable or the REST API.

Does deleting a service account key revoke access immediately?

It stops new authentication with the key, but access tokens already issued from it stay valid until they expire (one hour by default). To revoke those, disable or delete the service account.

Can I recover a deleted service account key?

No. Deleting is permanent, which is why Google recommends disabling the key first and deleting it only once you're sure nothing needs it.

How often should I rotate service account keys?

Google recommends at least every 90 days, and immediately if you suspect a key was exposed. Better still, replace keys with attached service accounts or Workload Identity Federation.

Why can't I create a service account key?

Your organization probably enforces iam.disableServiceAccountKeyCreation, which is on by default for organizations created on or after May 3, 2024. An administrator can exempt a project with a tag.

Sources

  1. Google Cloud IAM: Service account key rotation
  2. Google Cloud IAM: Create and delete service account keys
  3. Google Cloud IAM: Disable and enable service account keys
  4. Google Cloud IAM: List and get service account keys
  5. Google Cloud IAM: Best practices for managing service account keys
  6. Google Cloud IAM: Service account credentials
Rotate a Google Cloud Service Account Key Safely