When developing applications using Google Cloud Run or Firebase, it’s easy to forget that every time you deploy a new version of your service, a new revision gets created. Over time, these revisions can accumulate — consuming storage, cluttering your workspace, and potentially increasing costs.
If you frequently deploy updates, you might end up with dozens or even hundreds of unused revisions. Cleaning them manually can be tedious and error-prone. Thankfully, you can automate the cleanup of old revisions safely and efficiently using simple scripts or Cloud Scheduler jobs.
In this post, you’ll learn how to automatically manage and delete inactive revisions from Cloud Run and Firebase to keep your cloud environment clean and cost-effective.
Understanding Cloud Run Revisions
Every time you deploy a new version of your app in Cloud Run, Google Cloud creates a new revision. A revision is like a snapshot of your service at a particular point in time — including its code, configuration, and environment variables.
Cloud Run keeps old revisions so you can roll back if something goes wrong. However, unless you actively manage them, these old revisions continue to exist indefinitely.
For example:
-
You deploy an update every day.
-
After a month, you have 30+ revisions, but only the latest one is actively serving traffic.
-
The other 29 remain idle but occupy storage and clutter your dashboard.
The same situation occurs with Firebase Extensions or Cloud Functions, where older versions linger even after new deployments.
Why Cleaning Up Revisions Matters
-
Reduced Costs: Old revisions may consume storage and related resources.
-
Better Performance: Fewer inactive revisions mean faster deployments and less clutter.
-
Security: Outdated versions may use older dependencies or environment variables that shouldn’t be retained.
-
Automation Readiness: Regular cleanup supports efficient CI/CD pipelines.
By setting up an automatic cleanup routine, you ensure your environment remains optimized and easy to maintain.
Step 1: Identify Unused Cloud Run Revisions
Before deleting, it’s important to confirm which revisions are inactive.
Run the following command in your Cloud Shell:
This lists all revisions, their associated services, traffic percentage, and creation time.
Revisions with 0% traffic are safe to delete — these are not serving any users.
Step 2: Manually Delete Inactive Revisions (Optional)
If you want to manually clean up old revisions, use this command:
Replace REVISION_NAME and REGION_NAME with your actual values.
For example:
This deletes a specific revision while keeping the active one intact.
Step 3: Automate the Cleanup with a Script
To make this process automatic, create a bash script that identifies and deletes all old revisions that have 0% traffic.
Here’s a sample cleanup script:
Save this file as cleanup-revisions.sh and make it executable:
Step 4: Schedule Automatic Cleanup
To make the cleanup run periodically, use Cloud Scheduler:
-
Open Google Cloud Console → Cloud Scheduler.
-
Click Create Job.
-
Set the frequency (for example, every week):
(This runs every Sunday at 2 AM.)
-
Set the target type to Pub/Sub and publish a message that triggers a Cloud Function or Cloud Run service which runs your cleanup script.
Alternatively, you can run the script on a Compute Engine VM or through your CI/CD pipeline (like Cloud Build or GitHub Actions).
Step 5: Cleanup Firebase Functions
If you use Firebase Hosting or Cloud Functions, similar cleanup can be applied. Older versions of Cloud Functions may remain after redeployment.
You can list them using:
And delete unused versions:
For large projects, automate this with a script that filters inactive or outdated function versions and removes them regularly.
Best Practices
-
Keep a few recent revisions (e.g., last 3) for rollback safety.
-
Avoid deleting active revisions that still serve traffic.
-
Use IAM roles carefully — ensure cleanup scripts only have permission to delete revisions, not other services.
-
Monitor regularly — track revisions count and cleanup logs to ensure stability.
Step 6: Optional — Use Labels for Easier Management
When deploying services, add labels to revisions like this:
Later, you can filter and clean up based on labels:
This helps you safely target older revisions for deletion.
Final Thoughts
Automating the cleanup of Cloud Run and Firebase revisions is a small but powerful step toward keeping your cloud infrastructure lean, efficient, and secure. By combining gcloud commands, scripts, and Cloud Scheduler, you can completely eliminate manual maintenance work.
The result?
A faster, cleaner, and more cost-effective deployment environment where you focus on building — not housekeeping.