You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写脚本批量删除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 gcloud CLI installed and initialized (run gcloud init if you haven't already)
  • Sufficient IAM permissions (specifically compute.instanceGroups.delete for zonal groups, and compute.instanceGroupsManaged.delete for 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:

  1. Save it as delete-all-zonal-igs.sh
  2. Make it executable: chmod +x delete-all-zonal-igs.sh
  3. 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-run flag 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-zones to see all zonal groups, or gcloud compute instance-groups managed list --all-regions for regional groups before running deletion scripts.

内容的提问来源于stack exchange,提问作者Yivgeny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:14:53