Eduarn – Online & Offline Training with Free LMS for Python, AI, Cloud & More

Monday, April 6, 2026

How to Delete All Resources in GCP Using Script with Logging | Complete DevOps Guide by Eduarn

 

How to Delete All Resources in GCP Using Script (With Owner Logging) | Eduarn DevOps Guide

Managing cloud resources efficiently is a critical skill in today’s DevOps world. While creating infrastructure using automation tools is common, cleaning up resources properly is equally important to avoid unnecessary costs.

In Google Cloud Platform, there is no single command to delete all running services. However, with the right scripting approach, you can automate the cleanup process safely.

At Eduarn.com, we train professionals on real-world Cloud, DevOps, and AI scenarios, and this is one of the most practical scripts used in training labs and enterprise environments.


🔍 Why Resource Cleanup Matters

Failing to delete unused resources can lead to:

❌ Unexpected billing
❌ Security risks
❌ Resource sprawl
❌ Compliance issues

👉 Automating cleanup ensures cost optimization and governance


🧑‍đŸ’ģ Prerequisites

Before running the script:

  • Install Google Cloud SDK
  • Authenticate:
gcloud auth login
  • Ensure you have Owner or sufficient IAM permissions

🧾 Complete GCP Cleanup Script (With Logging)

This script deletes major resources and logs actions for tracking:

#!/bin/bash

PROJECT_ID="your-project-id"
LOG_FILE="gcp_cleanup.log"

echo "Starting cleanup for project: $PROJECT_ID" | tee -a $LOG_FILE
gcloud config set project $PROJECT_ID

echo "Deleting Compute Engine instances..." | tee -a $LOG_FILE
gcloud compute instances list --format="value(name,zone)" | while read NAME ZONE; do
  echo "Deleting VM: $NAME in $ZONE" | tee -a $LOG_FILE
  gcloud compute instances delete $NAME --zone=$ZONE --quiet
done

echo "Deleting Cloud Storage buckets..." | tee -a $LOG_FILE
for BUCKET in $(gcloud storage buckets list --format="value(name)"); do
  echo "Deleting bucket: $BUCKET" | tee -a $LOG_FILE
  gcloud storage buckets delete $BUCKET --quiet
done

echo "Deleting BigQuery datasets..." | tee -a $LOG_FILE
for DATASET in $(bq ls --format=prettyjson | jq -r '.[].datasetReference.datasetId'); do
  echo "Deleting dataset: $DATASET" | tee -a $LOG_FILE
  bq rm -r -f -d $PROJECT_ID:$DATASET
done

echo "Deleting Cloud SQL instances..." | tee -a $LOG_FILE
for SQL_INSTANCE in $(gcloud sql instances list --format="value(name)"); do
  echo "Deleting SQL instance: $SQL_INSTANCE" | tee -a $LOG_FILE
  gcloud sql instances delete $SQL_INSTANCE --quiet
done

echo "Deleting Cloud Functions..." | tee -a $LOG_FILE
for FUNC in $(gcloud functions list --format="value(name)"); do
  echo "Deleting function: $FUNC" | tee -a $LOG_FILE
  gcloud functions delete $FUNC --quiet
done

echo "Deleting Cloud Run services..." | tee -a $LOG_FILE
for SERVICE in $(gcloud run services list --platform managed --format="value(metadata.name)"); do
  echo "Deleting service: $SERVICE" | tee -a $LOG_FILE
  gcloud run services delete $SERVICE --platform managed --quiet
done

echo "Deleting Pub/Sub topics..." | tee -a $LOG_FILE
for TOPIC in $(gcloud pubsub topics list --format="value(name)"); do
  echo "Deleting topic: $TOPIC" | tee -a $LOG_FILE
  gcloud pubsub topics delete $TOPIC --quiet
done

echo "Deleting GKE clusters..." | tee -a $LOG_FILE
for CLUSTER in $(gcloud container clusters list --format="value(name,zone)"); do
  NAME=$(echo $CLUSTER | awk '{print $1}')
  ZONE=$(echo $CLUSTER | awk '{print $2}')
  echo "Deleting cluster: $NAME in $ZONE" | tee -a $LOG_FILE
  gcloud container clusters delete $NAME --zone $ZONE --quiet
done

echo "Cleanup completed for project: $PROJECT_ID" | tee -a $LOG_FILE

🔐 Owner Logging & Audit Benefits

This script includes logging using:

tee -a gcp_cleanup.log

Benefits:

  • Track who deleted what
  • Maintain audit trail
  • Useful for compliance & enterprise governance
  • Debug failed deletions

⚠️ Important Considerations

  • Ensure you have Owner or Admin role
  • Some resources (like VPCs, firewall rules) may need additional handling
  • Always test in a non-production environment first

💡 Pro Tip from Eduarn.com

👉 If your goal is to delete everything quickly:

gcloud projects delete PROJECT_ID

But use scripts when:

  • You want selective cleanup
  • You need audit logs
  • You are automating DevOps workflows

🎓 Learn Real DevOps with Eduarn.com

At Eduarn.com, we provide:

✔ Hands-on Google Cloud & DevOps training
✔ Real-time project-based learning
✔ AI + Automation programs
✔ Corporate & Retail training solutions

👉 Visit: www.eduarn.com


🚀 Final Thoughts

Automation is not just about creating infrastructure—it’s about managing and cleaning it efficiently.

👉 Smart engineers don’t just build—they optimize and control costs.


đŸ’Ŧ Have you automated your cloud cleanup yet?

#GCP #DevOps #CloudAutomation #GoogleCloud #IaC #Eduarn #CloudTraining #FinOps #Automation

No comments:

Post a Comment

Hands-on Learning vs Theoretical Learning: Which One Actually Gets You Hired in 2026?

đŸŽ¯ Introduction: The Real Problem Nobody Talks About You studied for years. You passed exams. You collected certificates. But when it’s ...