#!/bin/bash # Check if AWS CLI is installed if ! command -v aws &> /dev/null then echo "AWS CLI not found. Please install and configure the AWS CLI." exit 1 fi # Check if ECR_REPO_NAME is set if [ -z "$ECR_REPO_NAME" ]; then echo "ECR_REPO_NAME environment variable is not set." exit 1 fi # Delete all images in the ECR repository image_ids=$(aws ecr list-images --repository-name "$ECR_REPO_NAME" --query 'imageIds[*]' --output json) if [ "$image_ids" != "[]" ]; then echo "Deleting all images in the ECR repository '$ECR_REPO_NAME'..." aws ecr batch-delete-image --repository-name "$ECR_REPO_NAME" --image-ids "$image_ids" if [ $? -eq 0 ]; then echo "All images in the ECR repository '$ECR_REPO_NAME' have been deleted successfully." else echo "Failed to delete images in the ECR repository '$ECR_REPO_NAME'." exit 1 fi else echo "No images found in the ECR repository '$ECR_REPO_NAME'." fi # Delete the ECR repository echo "Deleting the ECR repository '$ECR_REPO_NAME'..." aws ecr delete-repository --repository-name "$ECR_REPO_NAME" --force if [ $? -eq 0 ]; then echo "ECR repository '$ECR_REPO_NAME' has been deleted successfully." else echo "Failed to delete ECR repository '$ECR_REPO_NAME'." exit 1 fi