Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnybradshaw/ee22cf138375b38c35877ffc9b54c616 to your computer and use it in GitHub Desktop.
Save johnybradshaw/ee22cf138375b38c35877ffc9b54c616 to your computer and use it in GitHub Desktop.
Delete all versions of all files in s3 versioned bucket using AWS CLI and jq.
#!/bin/bash
#delete versions from a bucket, this will work with Scaleway
bucket=$1
location=$2
endpoint=".scw.cloud"
set -e
set -x
echo "Removing all versions from $bucket"
versions=`aws s3api list-object-versions --bucket $bucket --endpoint-url https://s3.${location}${endpoint} |jq '.Versions'`
markers=`aws s3api list-object-versions --bucket $bucket --endpoint-url https://s3.${location}${endpoint}|jq '.DeleteMarkers'`
echo "removing files"
for version in $(echo "${versions}" | jq -r '.[] | @base64'); do
version=$(echo ${version} | base64 --decode)
key=`echo $version | jq -r .Key`
versionId=`echo $version | jq -r .VersionId `
cmd=`aws s3api delete-object --bucket $bucket --key "$key" --version-id $versionId --endpoint-url https://s3.${location}${endpoint}`
echo $cmd
$cmd
done
echo "removing delete markers"
for marker in $(echo "${markers}" | jq -r '.[] | @base64'); do
marker=$(echo ${marker} | base64 --decode)
key=`echo $marker | jq -r .Key`
versionId=`echo $marker | jq -r .VersionId `
cmd=`aws s3api delete-object --bucket $bucket --key "$key" --version-id $versionId --endpoint-url https://s3.${location}${endpoint}`
echo $cmd
$cmd
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment