How to Automatically Cleanup Revisions in Cloud Run Functions & Firebase

Published November 12, 2025
Written by Nishant Kumar Yogesh
Expertise Software Engineer
1 views

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

  1. Reduced Costs: Old revisions may consume storage and related resources.

  2. Better Performance: Fewer inactive revisions mean faster deployments and less clutter.

  3. Security: Outdated versions may use older dependencies or environment variables that shouldn’t be retained.

  4. 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:

gcloud run revisions list --format="table(name, service, traffic, creationTimestamp)"

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:

gcloud run revisions delete REVISION_NAME --region REGION_NAME

Replace REVISION_NAME and REGION_NAME with your actual values.

For example:

gcloud run revisions delete myservice-00015-abc --region asia-south1

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:

#!/bin/bash REGION="asia-south1" SERVICE="myservice" echo "Fetching inactive revisions for $SERVICE in $REGION..." # List all revisions with zero traffic INACTIVE_REVISIONS=$(gcloud run revisions list --service $SERVICE --region $REGION \ --filter="traffic=0" --format="value(metadata.name)") for REV in $INACTIVE_REVISIONS do echo "Deleting revision: $REV" gcloud run revisions delete $REV --region $REGION --quiet done echo "Cleanup complete!"

Save this file as cleanup-revisions.sh and make it executable:

chmod +x cleanup-revisions.sh

Step 4: Schedule Automatic Cleanup

To make the cleanup run periodically, use Cloud Scheduler:

  1. Open Google Cloud Console → Cloud Scheduler.

  2. Click Create Job.

  3. Set the frequency (for example, every week):

    0 2 * * 0

    (This runs every Sunday at 2 AM.)

  4. 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:

gcloud functions list

And delete unused versions:

gcloud functions delete FUNCTION_NAME

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:

gcloud run deploy myservice --image gcr.io/project/app --labels="env=prod,build=v1.3"

Later, you can filter and clean up based on labels:

gcloud run revisions list --filter="labels.build<v1.2"

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.

Nishant Kumar Yogesh

Software Engineer at Yourskart Technologies

Sharing insights on web development, SEO, digital marketing, and technology trends.