#!/bin/bash # Script to clean up old Google Cloud Functions deployment files BUCKET_NAME="gcf-v2-uploads-245796323861.us-central1.cloudfunctions.appspot.com" echo "=== Google Cloud Functions Bucket Cleanup ===" echo "Bucket: $BUCKET_NAME" echo "Date: $(date)" echo "" # Check if gcloud is authenticated if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q .; then echo "โŒ Not authenticated with gcloud. Please run: gcloud auth login" exit 1 fi echo "๐Ÿ“Š Current bucket size:" gsutil du -sh "gs://$BUCKET_NAME" echo "" echo "๐Ÿ“‹ Number of deployment files:" gsutil ls "gs://$BUCKET_NAME" | wc -l echo "" echo "๐Ÿ” Recent deployments (last 5):" echo "===============================" gsutil ls -lh "gs://$BUCKET_NAME" | tail -5 echo "" echo "โš ๏ธ WARNING: This will delete old deployment files!" echo " Only recent deployments will be kept for safety." echo "" read -p "Do you want to proceed with cleanup? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "โŒ Cleanup cancelled." exit 0 fi echo "" echo "๐Ÿงน Starting cleanup..." # Get list of all files, sort by date (oldest first), and keep only the last 3 echo "๐Ÿ“‹ Files to be deleted:" gsutil ls -l "gs://$BUCKET_NAME" | sort -k2 | head -n -3 | while read -r line; do if [[ $line =~ gs:// ]]; then filename=$(echo "$line" | awk '{print $NF}') echo " Will delete: $filename" fi done echo "" echo "๐Ÿ—‘๏ธ Deleting old files..." # Delete all but the last 3 files gsutil ls "gs://$BUCKET_NAME" | sort | head -n -3 | while read -r file; do echo " Deleting: $file" gsutil rm "$file" done echo "" echo "โœ… Cleanup completed!" echo "" echo "๐Ÿ“Š New bucket size:" gsutil du -sh "gs://$BUCKET_NAME" echo "" echo "๐Ÿ“‹ Remaining files:" gsutil ls -lh "gs://$BUCKET_NAME"