It is possible to restore a mistakenly deleted API Key using gcloud alpha services api-keys undelete. This method works on Cloud Shell, your local machine, or normal Compute Engine instances (which have the Cloud SDK pre-installed):
- Local machine only: Install the Google Cloud SDK (if it isn't already installed).
- Run and authorize Cloud SDK with a project owner account. (Must be a human account. Service accounts are not supported):
gcloud auth login
- Set the Google Cloud Project where the deleted API key is configured:
gcloud config set project PROJECT_ID
- Identify the deleted API key. List all API Keys in the project and find the API Key/s with the deleteTime property:
gcloud services api-keys list --show-deleted
Example response of a recently deleted API Key:
createTime: '2022-08-17T18:46:39.411227Z'
Note that above example does not show the actual API key string ("AizaSy..."). What it provides is the API key resource name which looks like "projects/<PROJECT_NUMBER>/..../keys/<KEY_ID>". This is what you'll need on the next step.
deleteTime: '2022-08-17T19:11:49.799445Z'
displayName: API_Key_Delete_Restore
etag: W/"9FKFGLbdvOw6EmcGAffYpw=="
name: projects/843465515130/locations/global/keys/cdc2364b-5bf2-4f3c-8c0e-f037229aed87
restrictions:
apiTargets:
- service: geocoding_backend
- service: geolocation
browserKeyRestrictions:
allowedReferrers:
- '*.woolpert.com/*'
uid: cdc2364b-5bf2-4f3c-8c0e-f037229aed87
updateTime: '2022-08-17T19:11:49.990925Z' - Copy the API key resource name and use it to undelete the API key:
gcloud alpha services api-keys undelete <API key resource name>
This restores your API Key including the application and API restrictions you have applied to it.
IMPORTANT: API Keys that are deleted will be retained in the system for 30 days. Keys beyond the 30-day retention window cannot be undeleted.
Comments
0 comments
Please sign in to leave a comment.