Delete Buckets and List Resources based on tags

Here is an easy couple of sets of commands to list all resources with a certain tag and delete all buckets with a certain tag. Certain beats manually deleting buckets with lots of contents. I got these little snippets from this AWS Workshop: https://workshops.aws/card/unauthorized

List resources with a certain tag:

aws resourcegroupstaggingapi get-resources --tag-filters Key=MY_TAG_KEY,Values=MY_TAG_VALUE --query 'ResourceTagMappingList[].ResourceARN'

Delete buckets with a certain tag:

aws resourcegroupstaggingapi get-resources --tag-filters Key=MY_TAG_KEY,Values=MY_TAG_VALUE --query 'ResourceTagMappingList[].ResourceARN' > buckets.json

((for bucket in {0..1}; do cat buckets.json |jq -r ".[$bucket]";done) |cut -d":" -f 6) > buckets-to-delete

while read line; do aws s3 rb s3://$line --force; done < buckets-to-delete

rm buckets.json

rm buckets-to-delete

Leave a comment

Blog at WordPress.com.

Up ↑