Skip to content

Instantly share code, notes, and snippets.

@alastairhm
Created January 21, 2022 14:33
Show Gist options
  • Save alastairhm/85d5724770418065252e69882f61ec2e to your computer and use it in GitHub Desktop.
Save alastairhm/85d5724770418065252e69882f61ec2e to your computer and use it in GitHub Desktop.
AWS CLI delete all but last five EBS snapshots with a certain name
#!/usr/bin/env bash
#
# Delete all but the last 5 snapshots with passed tag.
SNAPSHOTSID=$(aws ec2 describe-snapshots \
--owner-ids self \
--filters Name=tag:Name,Values="${1}" \
--query "Snapshots[*].{ID:SnapshotId}" --output text|tail +5)
for id in $SNAPSHOTSID; do
aws ec2 delete-snapshot --snapshot-id "$id"
echo "Successfully deleted snapshot $id"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment