Last active
November 22, 2017 10:03
-
-
Save ivystopia/cd97da9324c3beda05807681591f08f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# Delete all objects and versions from an S3 bucket | |
# including objects with spaces in their names | |
# Does not also delete the bucket, you must do this with aws s3 rb | |
# IMPORTANT: Disable versioning on the bucket before running this script | |
# otherwise when the object is deleted, a new version will be created | |
# Usage: ./delete_s3.sh bucket_name [aws_profile] | |
bucket=$1 | |
profile=${2:-default} | |
set -e | |
echo "Removing all versions from $bucket" | |
OIFS="$IFS" ; IFS=$'\n' ; oset="$-" ; set -f | |
while IFS="$OIFS" read -a line | |
do | |
key=`echo ${line[0]} | sed 's#SPACEREPLACE# #g'` | |
versionId=${line[1]} | |
echo "key: ${key} versionId: ${versionId}" | |
cmd="aws --profile=$profile s3api delete-object --bucket $bucket --key \"$key\" --version-id $versionId" | |
echo $cmd | |
eval $cmd | |
done < <(aws --profile=$profile s3api list-object-versions --bucket $bucket --query "[Versions,DeleteMarkers][].{Key: Key, VersionId: VersionId}" --output text | sed 's# #SPACEREPLACE#g' ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment