Skip to content

Instantly share code, notes, and snippets.

@ppp0
Forked from weavenet/delete_all_object_versions.sh
Last active August 30, 2017 15:42
Show Gist options
  • Save ppp0/3d7a57f9470a2a513ad605c6fffcbf8c to your computer and use it in GitHub Desktop.
Save ppp0/3d7a57f9470a2a513ad605c6fffcbf8c 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
bucket=$1
prefix=$2
max_entries=${3:-50}
set -e
echo "Removing ${max_entries} versions from $bucket"
echo "executing aws s3api list-object-versions --bucket $bucket --prefix $prefix --max-items $max_entries"
objects=$(aws s3api list-object-versions --bucket $bucket --prefix $prefix --max-items $max_entries)
versions=$(echo "${objects}" | jq '.Versions')
markers=$(echo "${objects}" | 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}"
echo "Launching $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}"
echo "Launching $cmd"
$cmd &
done
for job in $(jobs -p); do
wait $job
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment