Last active
October 25, 2018 04:09
-
-
Save veggiemonk/661a7867c909a228e45084fdbfd5ba83 to your computer and use it in GitHub Desktop.
delete versioned s3 bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
print_usage(){ | |
echo "USAGE:" | |
echo | |
echo "PROFILE=customer ./delete-s3-bucket.sh [list | all | 'bucket_name']" | |
echo | |
echo | |
} | |
profile=${PROFILE:-"default"} | |
echo "The AWS profile is $profile" | |
empty_bucket() { | |
echo | |
echo "Emptying bucket: $1" | |
del_script_ver="$1-ver.sh" | |
rm -f "$del_script_ver" | |
echo '#!/bin/bash' > "$del_script_ver" | |
echo 'set -x' >> "$del_script_ver" | |
echo "Generating delete versions script: $del_script_ver" | |
aws --output text s3api list-object-versions --bucket "$1" --profile "$2" \ | |
| grep -E "^VERSIONS" \ | |
| awk -v profile="$2" -v bucket_name="$1" '{print "aws s3api delete-object --bucket "bucket_name" --key "$4" --version-id "$8" --profile "profile";"}' >> "$del_script_ver"; | |
echo "DELETING VERSIONS" | |
# shellcheck disable=SC1090 | |
. "$del_script_ver" | |
del_script_mark="$1-ver.sh" | |
rm -f "$del_script_mark" | |
echo '#!/bin/bash' > "$del_script_mark" | |
echo 'set -x' >> "$del_script_mark" | |
echo "Generating delete markers script: $del_script_mark" | |
aws --output text s3api list-object-versions --bucket "$1" --profile "$2" \ | |
| grep -E "^DELETEMARKERS" \ | |
| grep -v "null" \ | |
| awk -v profile="$2" -v bucket_name="$1" '{print "aws s3api delete-object --bucket "bucket_name" --key "$3" --version-id "$5" --profile "profile";"}' >> "$del_script_mark"; | |
echo "DELETING MARKERS" | |
# shellcheck disable=SC1090 | |
. "$del_script_mark" | |
echo "done" | |
} | |
if [[ "$1" = "list" || "$#" -eq 0 ]]; then | |
print_usage | |
echo "The buckets are: " | |
aws s3 ls --profile "$profile" | awk '{print $3}' | |
elif [[ "$1" = "all" ]]; then | |
echo "The buckets are: " | |
echo | |
all_buckets=$(aws s3 ls --profile "$profile" | awk '{print $3}') | |
echo "$all_buckets" | |
echo | |
echo "Emptying all buckets." | |
for bucket in $all_buckets | |
do | |
if [ "veeps-hosting-main-terraform-state" = "$bucket" ]; then | |
echo | |
echo "This script is trying to delete the bucket for Terraform state. Skipping" | |
else | |
empty_bucket "$bucket" "$profile" | |
echo "Deleting empty bucket: $bucket" | |
aws s3 rb s3://"$bucket" --force --profile "$profile" | |
fi | |
done | |
else | |
echo "Deleting $1." | |
echo "Profile: $profile ." | |
empty_bucket "$1" "$profile" | |
aws s3 rb s3://"$1" --force --profile "$profile" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should delete any bucket of any size with or without versioning enabled.