Skip to content

Instantly share code, notes, and snippets.

@serkanh
Created August 15, 2017 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serkanh/f63883f02e3646d36e0e7208b2a260cd to your computer and use it in GitHub Desktop.
Save serkanh/f63883f02e3646d36e0e7208b2a260cd to your computer and use it in GitHub Desktop.
#!/bin/bash
#original https://gist.github.com/weavenet/f40b09847ac17dd99d16
bucket=$1
set -e
echo "Removing all versions from $bucket"
versions=`aws --region=us-west-2 s3api list-object-versions --bucket $bucket |jq '.Versions'`
markers=`aws --region=us-west-2 s3api list-object-versions --bucket $bucket |jq '.DeleteMarkers'`
let count=`echo $versions |jq 'length'`-1
if [ $count -gt -1 ]; then
echo "removing files"
for i in $(seq 0 $count); do
key=`echo $versions | jq .[$i].Key |sed -e 's/\"//g'`
versionId=`echo $versions | jq .[$i].VersionId |sed -e 's/\"//g'`
cmd="aws --region=us-west-2 s3api delete-object --bucket $bucket --key $key --version-id $versionId"
echo $cmd
$cmd
done
fi
let count=`echo $markers |jq 'length'`-1
if [ $count -gt -1 ]; then
echo "removing delete markers"
for i in $(seq 0 $count); do
key=`echo $markers | jq .[$i].Key |sed -e 's/\"//g'`
versionId=`echo $markers | jq .[$i].VersionId |sed -e 's/\"//g'`
cmd="aws --region=us-west-2 s3api delete-object --bucket $bucket --key $key --version-id $versionId"
echo $cmd
$cmd
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment