Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Batch Empty and Delete S3 Bucket with Grep Pattern
#!/usr/bin/env bash
# Sometimes we create a lot of S3 Bucket with similar names.
# The command below will find all buckets in the region with pattern `bucket_pattern`,
# then empty the bucket and then delete the bucket.
BUCKET_PATTERN=bucket_pattern
aws s3api list-buckets |\
jq -r '.Buckets[].Name' |\
grep $BUCKET_PATTERN |\
xargs -n 1 -I {} sh -c 'aws s3 rm s3://{} --recursive && aws s3 rb s3://{}'
#!/usr/bin/env bash
# Sometimes we create a lot of S3 Bucket with similar names.
# The command below will find all buckets in the region with pattern `bucket_pattern`,
# then empty the bucket and then delete the bucket.
BUCKET_PATTERN=bucket_pattern
aws s3api list-buckets |\
jq -r '.Buckets[].Name' |\
grep $BUCKET_PATTERN |\
xargs -n 1 -I {} sh -c 'aws s3 rm s3://{} --recursive'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment