如何编写脚本批量删除Google Cloud所有可用区的实例组?
Hey there! Let's walk through how to create scripts for deleting GCP instance groups—whether you need to handle interactive zone/region prompts or wipe out instance groups across all zones.
Prerequisites First
Before diving into scripts, make sure you have:
- The
gcloudCLI installed and initialized (rungcloud initif you haven't already) - Sufficient IAM permissions (specifically
compute.instanceGroups.deletefor zonal groups, andcompute.instanceGroupsManaged.deletefor regional groups) - Confirmed your target project with
gcloud config get-value project
Script 1: Delete Instance Groups Across ALL Zones
This bash script will scan every available zone in your project, list all zonal instance groups, and delete them (with optional quiet mode to skip confirmations).
#!/bin/bash # Optional: Hardcode your project ID if you don't want to use the configured one # PROJECT_ID="your-target-project-id" # gcloud config set project $PROJECT_ID # Fetch the currently configured project ID PROJECT_ID=$(gcloud config get-value project) if [ -z "$PROJECT_ID" ]; then echo "Error: No project configured. Run 'gcloud config set project <project-id>' first." exit 1 fi # Get all available zones in the project ZONES=$(gcloud compute zones list --format="value(name)") echo "=== Starting deletion of all zonal instance groups in project: $PROJECT_ID ===" for ZONE in $ZONES; do echo "Processing zone: $ZONE" # Fetch all zonal instance groups in the current zone INSTANCE_GROUPS=$(gcloud compute instance-groups list --zone $ZONE --format="value(name)") if [ -z "$INSTANCE_GROUPS" ]; then echo " No instance groups found here. Moving on." continue fi for IG in $INSTANCE_GROUPS; do echo " Deleting instance group: $IG" # Use --quiet to skip confirmation prompts; remove it if you want manual approval gcloud compute instance-groups delete $IG --zone $ZONE --quiet if [ $? -eq 0 ]; then echo " ✅ Successfully deleted $IG" else echo " ❌ Failed to delete $IG" fi done done echo "=== All zones processed ==="
How to Use This Script:
- Save it as
delete-all-zonal-igs.sh - Make it executable:
chmod +x delete-all-zonal-igs.sh - Run it:
./delete-all-zonal-igs.sh
Note for Regional Instance Groups:
If you need to delete regional managed instance groups (not tied to a single zone), modify the script to iterate over regions instead:
# Replace the ZONES section with this for regional groups REGIONS=$(gcloud compute regions list --format="value(name)") for REGION in $REGIONS; do echo "Processing region: $REGION" INSTANCE_GROUPS=$(gcloud compute instance-groups managed list --region $REGION --format="value(name)") if [ -z "$INSTANCE_GROUPS" ]; then echo " No regional instance groups found here. Moving on." continue fi for IG in $INSTANCE_GROUPS; do echo " Deleting regional instance group: $IG" gcloud compute instance-groups managed delete $IG --region $REGION --quiet done done
Script 2: Interactive Prompt for Zone/Region Selection
If you want a script that prompts you to choose a zone, region, or full cleanup when run without arguments, this one fits the bill:
#!/bin/bash PROJECT_ID=$(gcloud config get-value project) if [ -z "$PROJECT_ID" ]; then echo "Error: No project configured. Run 'gcloud config set project <project-id>' first." exit 1 fi # Check if user provided a zone/region argument if [ $# -eq 0 ]; then echo "=== Choose an option ===" echo "1. Delete instance groups in a specific zone" echo "2. Delete regional managed instance groups in a specific region" echo "3. Delete ALL zonal instance groups across every zone" read -p "Enter your choice (1/2/3): " CHOICE case $CHOICE in 1) read -p "Enter the zone name (e.g., us-central1-a): " ZONE INSTANCE_GROUPS=$(gcloud compute instance-groups list --zone $ZONE --format="value(name)") for IG in $INSTANCE_GROUPS; do gcloud compute instance-groups delete $IG --zone $ZONE done ;; 2) read -p "Enter the region name (e.g., us-central1): " REGION INSTANCE_GROUPS=$(gcloud compute instance-groups managed list --region $REGION --format="value(name)") for IG in $INSTANCE_GROUPS; do gcloud compute instance-groups managed delete $IG --region $REGION done ;; 3) # Reuse the full zone deletion logic from Script 1 ZONES=$(gcloud compute zones list --format="value(name)") for ZONE in $ZONES; do INSTANCE_GROUPS=$(gcloud compute instance-groups list --zone $ZONE --format="value(name)") for IG in $INSTANCE_GROUPS; do gcloud compute instance-groups delete $IG --zone $ZONE done done ;; *) echo "Invalid choice. Exiting." exit 1 ;; esac else # Auto-detect if argument is a zone (contains '-') or region if [[ $1 == *-* ]]; then ZONE=$1 echo "Deleting all instance groups in zone: $ZONE" INSTANCE_GROUPS=$(gcloud compute instance-groups list --zone $ZONE --format="value(name)") for IG in $INSTANCE_GROUPS; do gcloud compute instance-groups delete $IG --zone $ZONE done else REGION=$1 echo "Deleting all regional instance groups in region: $REGION" INSTANCE_GROUPS=$(gcloud compute instance-groups managed list --region $REGION --format="value(name)") for IG in $INSTANCE_GROUPS; do gcloud compute instance-groups managed delete $IG --region $REGION done fi fi
Critical Safety Notes
- Always test first: Add the
--dry-runflag to the delete commands (e.g.,gcloud compute instance-groups delete $IG --zone $ZONE --dry-run) to simulate deletions without making changes. - Instance deletion: Deleting a managed instance group will automatically destroy all instances in the group. If you need to keep instances, remove them from the group first with
gcloud compute instance-groups managed remove-instances. - Double-check resources: Run
gcloud compute instance-groups list --all-zonesto see all zonal groups, orgcloud compute instance-groups managed list --all-regionsfor regional groups before running deletion scripts.
内容的提问来源于stack exchange,提问作者Yivgeny

