Forked from weavenet/delete_all_object_versions.sh
Last active
July 19, 2017 01:10
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 | |
STARTD=${PWD} | |
SELFD=$(cd $(dirname ${0}) >/dev/null 2>&1; pwd) | |
SELF=$(basename ${0}) | |
SELFN=$(basename ${SELFD}) | |
SELFU=${SELF%.*} | |
SELFZ=${SELFD}/${SELF} | |
set -e | |
bucket=${1}; shift | |
[ "${bucket}" = 'x' ] && { | |
bucket=${1}; shift | |
kv=(${1/=/ }); shift | |
aws s3api delete-object --bucket ${bucket} --key ${kv[0]} --version-id ${kv[1]} | |
exit | |
} | |
prefix=${1}; shift | |
[ "${bucket}" ] || { | |
echo >&2 "usage: ${0} bucket [prefix]" | |
exit ${LINENO} | |
} | |
[ "${prefix}" ] && prefix="--prefix ${prefix}" | |
echo "Removing all versions from ${bucket}/${prefix}" | |
echo "removing files" | |
aws s3api list-object-versions --bucket ${bucket} ${prefix} \ | |
| jq -r '.Versions[] | .Key + " " + .VersionId' \ | |
| parallel -n2 bash ${SELFZ} x ${bucket} | |
echo "removing delete markers" | |
aws s3api list-object-versions --bucket ${bucket} ${prefix} \ | |
| jq -r '.DeleteMarkers[] | .Key + " " + .VersionId' \ | |
| parallel -n2 bash ${SELFZ} x ${bucket} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment