Skip to content

Instantly share code, notes, and snippets.

@petrabarus
Last active May 4, 2021 02:30
Show Gist options
  • Save petrabarus/5abed92bc487f36bebd8a05ec0d14a73 to your computer and use it in GitHub Desktop.
Save petrabarus/5abed92bc487f36bebd8a05ec0d14a73 to your computer and use it in GitHub Desktop.
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