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-deletedExample 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.
- service: geocoding_backend
- service: geolocation
- 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.