|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2018 The Kubernetes Authors. |
| 4 | +# Copyright 2022 Google LLC |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +# Script to delete all GCS buckets with a given prefix |
| 19 | + |
| 20 | +BUCKET_PREFIX="gcsfusecsi-testsuite-gen-" |
| 21 | + |
| 22 | +# List all buckets |
| 23 | +BUCKETS=$(gsutil ls) |
| 24 | + |
| 25 | +# Array to store buckets to be deleted |
| 26 | +DELETE_BUCKETS=() |
| 27 | + |
| 28 | +# Identify buckets for deletion and print them |
| 29 | +echo "The following buckets will be deleted:" |
| 30 | +for BUCKET in $BUCKETS; do |
| 31 | + # Extract the bucket name (remove gs://) |
| 32 | + BUCKET_NAME=$(echo $BUCKET | sed 's/gs:\/\///') |
| 33 | + |
| 34 | + # Check if the bucket starts with the specified prefix |
| 35 | + if [[ "$BUCKET_NAME" == "$BUCKET_PREFIX"* ]]; then |
| 36 | + echo "- $BUCKET_NAME" |
| 37 | + DELETE_BUCKETS+=("$BUCKET_NAME") # Add to array |
| 38 | + fi |
| 39 | +done |
| 40 | + |
| 41 | +# Check if there are any buckets to delete |
| 42 | +if [[ ${#DELETE_BUCKETS[@]} -eq 0 ]]; then |
| 43 | + echo "No buckets found with the prefix '$BUCKET_PREFIX'. Script exiting." |
| 44 | + exit 0 |
| 45 | +fi |
| 46 | + |
| 47 | +# Prompt for confirmation |
| 48 | +read -p "Are you sure you want to delete ALL the listed buckets? (y/n): " CONFIRM |
| 49 | + |
| 50 | +# If the user confirms, delete the buckets |
| 51 | +if [[ "$CONFIRM" == "y" || "$CONFIRM" == "Y" ]]; then |
| 52 | + echo "Deleting buckets..." |
| 53 | + for BUCKET_NAME in "${DELETE_BUCKETS[@]}"; do |
| 54 | + gsutil -m rm -r "gs://$BUCKET_NAME" |
| 55 | + echo "Deleted: $BUCKET_NAME" |
| 56 | + done |
| 57 | + echo "All specified buckets have been deleted." |
| 58 | +else |
| 59 | + echo "Deletion cancelled." |
| 60 | + exit 0 |
| 61 | +fi |
| 62 | + |
| 63 | +echo "Script completed." |
0 commit comments