Forked from weavenet/delete_all_object_versions.sh
Last active
March 4, 2022 09:23
-
-
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.
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 | |
#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