How to Automatically Cleanup Revisions in Cloud Run Functions & Firebase

Google Cloud offers several serverless services that let you build and run applications without having to manage servers. You can run containers on Google Cloud Run, create API endpoints with Cloud Run Functions (formerly Google Cloud Functions) or run your mobile and web backends with Firebase Functions.
A common concept for serverless functions on Google Cloud is the concept of revisions. Each time you deploy your code, a new and immutable revision is created on Google Cloud and the old revision is not deleted. This is useful because, later, you can easily roll back to an older version of a function if something goes wrong.
The old, inactive revisions do not handle any traffic and also do not incur any costs but it might be a good idea to regularly clean up these inactive revisions from your Google Cloud project. Regular cleanup will also ensure that your old revisions with bugs or deprecated code aren’t accidentally re-deployed.
Remove Inactive Revisions from Google Cloud Run
In one of my Google Cloud projects, there are about 250+ inactive revisions of a single Cloud Run function. Multiply that with several such services and you can see how it can add up to a lot of inactive revisions.
It is easy to manually remove an inactive revision. Inside the Google Cloud Console, go to Cloud Run Functions
and select your service. Go to the Revisions
tab and you will see all the revisions of that service. Click the 3-dot menu against any revision and select Delete
to permanently delete that revision.
Automatically Delete Cloud Run Revisions
While I can mannually delete the inactive revisions from the Google Cloud Console, it will take me a lot of time and effort to delete all the inactive revisions.
What we have here is a simple bash script that will delete all the inactive revisions of a Cloud Run service. The script uses the gcloud
command-line tool to list all the revisions of a Cloud Run service that are inactive.
It then iterates through the list of inactive revisions and deletes them one by one using the gcloud run revisions delete
command. The revisions are deleted in chronological order, starting from the oldest revision.
To get started, make a copy of the script and save it as cleanup-cloud-run-revisions.sh
. Next, replace the GOOGLE_PROJECT_ID
, GCP_REGION
and CLOUD_RUN_SERVICES
variables with your own values. We are using an array to store the names of the Cloud Run services that it can handle multiple services in a single run.
#!/bin/bash
## Google Cloud Project ID
GOOGLE_PROJECT_ID=PUT_YOUR_PROJECT_ID_HERE
## GCP Region
GCP_REGION=us-east1
## Array of Cloud Run services to clean up
CLOUD_RUN_SERVICES=(
"service_name_1"
"service_name_2"
)
for SERVICE in "${CLOUD_RUN_SERVICES[@]}"; do
echo "Processing service: $SERVICE"
# Get list of inactive revisions
REVISIONS=$(gcloud run revisions list \
--service="$SERVICE" \
--project="$GOOGLE_PROJECT_ID" \
--region="$GCP_REGION" \
--format='value(metadata.name)' \
--sort-by='metadata.creationTimestamp' \
--filter="status.conditions.type:Active AND status.conditions.status:'False'")
if [ -z "$REVISIONS" ]; then
echo "No inactive revisions found for $SERVICE"
continue
fi
REVISION_COUNT=$(echo "$REVISIONS" | wc -l)
echo "Found $REVISION_COUNT inactive revisions"
read -p "Confirm cleanup for $SERVICE? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "$REVISIONS" | while read -r REVISION; do
echo "Deleting revision: $REVISION"
gcloud run revisions delete "$REVISION" \
--quiet \
--project="$GOOGLE_PROJECT_ID" \
--region="$GCP_REGION"
done
echo "Completed cleanup for $SERVICE"
else
echo "Cleanup cancelled for $SERVICE"
fi
done
Automatically Delete Container Images in Artifact Registry
Please note that this shell script only will only delete the Cloud Run revisions. The underlying container images in Artifact Registry will not be deleted and they will continue to consume storage space thus incurring a recurring cost.
You can create a cleanup policy to automatically delete container images in Artifact Registry based on a schedule or state. For instance, your cleanup policy can delete container images that are older than 30 days or a policy that retains only the latest container images.