Skip to content

Instantly share code, notes, and snippets.

@scraly
Created January 3, 2018 13:07
Show Gist options
  • Save scraly/1a37b75665c2f9842a24d220b9091279 to your computer and use it in GitHub Desktop.
Save scraly/1a37b75665c2f9842a24d220b9091279 to your computer and use it in GitHub Desktop.
#!/bin/bash
###################################
#
# The aim of this script is to remove all versions of all files in AWS S3 versioned bucket using AWS CLI
#
###################################
bucket=$1
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'` # replace the TEMPTEXT by space again (needed to temp replace because of split by all spaces by read -a above)
versionId=${line[1]}
echo "key: ${key} versionId: ${versionId}"
# use doublequotes (escaped) around the key to allow for spaces in the key.
cmd="aws s3api delete-object --bucket $bucket --key \"$key\" --version-id $versionId"
echo $cmd
eval $cmd
done < <(aws 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