#!/bin/bash set -e # Container Image Cleanup Script # This script cleans up container images from Google Container Registry # Images are not managed by Terraform, so this provides a manual cleanup option PROJECT_ID=$(gcloud config get-value project) IMAGE_NAME="sereact-api" if [ -z "$PROJECT_ID" ]; then echo "ERROR: No Google Cloud project is set. Run 'gcloud config set project YOUR_PROJECT_ID'" exit 1 fi echo "Cleaning up container images for project: $PROJECT_ID" echo "Image repository: gcr.io/$PROJECT_ID/$IMAGE_NAME" echo "" # Check if repository exists if ! gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" > /dev/null 2>&1; then echo "No container images found for $IMAGE_NAME" exit 0 fi echo "Found container images. Listing current images:" gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" echo "" read -p "Do you want to delete ALL images for $IMAGE_NAME? (yes/no): " confirm if [ "$confirm" != "yes" ]; then echo "Cleanup cancelled." exit 0 fi echo "Deleting container images..." # Get all image digests and delete them DIGESTS=$(gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" --format="get(digest)" --filter="tags:*" 2>/dev/null || true) UNTAGGED_DIGESTS=$(gcloud container images list-tags "gcr.io/$PROJECT_ID/$IMAGE_NAME" --format="get(digest)" --filter="-tags:*" 2>/dev/null || true) # Delete tagged images if [ ! -z "$DIGESTS" ]; then echo "Deleting tagged images..." for digest in $DIGESTS; do gcloud container images delete "gcr.io/$PROJECT_ID/$IMAGE_NAME@$digest" --force-delete-tags --quiet || echo "Failed to delete $digest" done fi # Delete untagged images if [ ! -z "$UNTAGGED_DIGESTS" ]; then echo "Deleting untagged images..." for digest in $UNTAGGED_DIGESTS; do gcloud container images delete "gcr.io/$PROJECT_ID/$IMAGE_NAME@$digest" --quiet || echo "Failed to delete $digest" done fi echo "Container image cleanup completed." echo "" echo "Note: The repository gcr.io/$PROJECT_ID/$IMAGE_NAME may still exist but should be empty." echo "You can verify with: gcloud container images list-tags gcr.io/$PROJECT_ID/$IMAGE_NAME"