Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete all versions of all files in s3 versioned bucket using AWS CLI and jq.
#!/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