Skip to content

Instantly share code, notes, and snippets.

@Siddhant-K-code
Created April 28, 2024 04:03
Show Gist options
  • Save Siddhant-K-code/0b26e00a6d0891db4058033e5766376a to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/0b26e00a6d0891db4058033e5766376a to your computer and use it in GitHub Desktop.
AWS S3 Bucket bulk deletion

AWS S3 Bucket bulk deletion

To utilize the AWS CLI for listing S3 buckets and deleting those that match a specific pattern, follow these steps. This guide focuses on how to remove buckets starting with the prefix "sk."

Requirements

  • AWS CLI must be installed.
  • Appropriate AWS credentials and permissions should be set up.

Step 1: Get a List of Buckets First, use the AWS CLI installed on your machine to retrieve a list of all S3 buckets.

aws s3 ls

Step 2: Delete Buckets Matching a Specific Pattern To delete buckets beginning with "sk," you can use a shell script to filter out and remove these buckets.

The script below identifies bucket names starting with "sk" and proceeds to delete each one. Note: This script will remove the bucket and all its contents. Ensure you have backed up your data beforehand.

aws s3 ls | awk '{print $3}' | grep '^sk' | while read bucket
do
    echo "Deleting bucket $bucket..."
    aws s3 rb s3://$bucket --force
done

Script Functions:

  • aws s3 ls: Lists all buckets.
  • awk '{print $3}': Extracts only the bucket names.
  • grep '^sk': Filters for bucket names starting with "sk."
  • while read bucket; do ... done: Deletes each matched bucket in a loop.

Caution

  • Ensure you have backed up any necessary data before proceeding with deletion.
  • Use the --force option in aws s3 rb to remove a bucket and all its contents if it's not empty.
  • It is advisable to insert an echo statement before executing the delete command to confirm the names of the buckets being deleted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment