#!/bin/bash set -x echo "Are you really sure as AMIs might be used by customers !!" read TAG_FILTER="Name=tag:Name,Values=*-uefi-*" # TAG_FILTER="Name=tag:Name,Values=zdt-alpine-3.16.2-x86_64-bios-tiny-kubezero-1.23.10-r0" # TAG_FILTER="Name=tag:Name,Values=zdt-alpine-3.16.2-x86_64-bios-tiny-minimal-r2" #for r in $(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text); do for r in eu-central-1 us-west-2 ap-southeast-2 ca-central-1 us-east-1 us-west-1; do amis=$(aws ec2 describe-images --region $r --owners self --output json --filters $TAG_FILTER | jq -r '.Images[].ImageId') for a in $amis; do aws ec2 deregister-image --region $r --image-id $a && echo "Deleted AMI $a in $r" done amis=$(aws ec2 describe-images --region $r --owners self --output json --filters Name=state,Values=failed | jq -r '.Images[].ImageId') for a in $amis; do aws ec2 deregister-image --region $r --image-id $a && echo "Deleted AMI $a in $r" done snapshots=$(aws ec2 describe-snapshots --region $r --owner-ids self --output json --filters $TAG_FILTER | jq -r '.Snapshots[].SnapshotId') for s in $snapshots; do aws ec2 delete-snapshot --snapshot-id $s --region $r && echo "Deleted snapshot $s in $r" done done