Skip to content

Instantly share code, notes, and snippets.

@djosephsen
Created December 7, 2018 23:39
Show Gist options
  • Save djosephsen/f45c02f7085c47050fae85fb6aa76b91 to your computer and use it in GitHub Desktop.
Save djosephsen/f45c02f7085c47050fae85fb6aa76b91 to your computer and use it in GitHub Desktop.
Delete all previous versions of an object on S3 except the most recent
#!/usr/bin/env bash
BUCKET=${1}
PREFIX=${2}
function error {
echo "ERROR:: ${@}"
exit 2
}
function warn {
echo "WARNING:: ${@}"
}
function debug {
if [ -n "${DEBUG}" ]
then
echo "DEBUG:: ${@}"
fi
}
function info {
echo "INFO:: ${@}"
}
debug "Checking for jq binary"
which jq > /dev/null || error "No jq binary found"
debug "Checking for bucket"
[[ "${BUCKET}" ]] || error "No bucket defined"
debug "Checking for prefix"
if [ -z "${PREFIX}" ]
then
warn "No prefix (\$2) given, recursive delete on entire bucket: ${BUCKET}"
else
PREFIX="--prefix ${PREFIX}"
fi
VERSIONS=$(aws s3api list-object-versions --bucket ${BUCKET} ${PREFIX})
if [ -n "${VERSIONS}" ]
then
VERS=$(echo ${VERSIONS}| jq '.Versions[] | select(.IsLatest | not)')
if echo ${VERSIONS} | grep -q '.DeleteMarkers'
then
DELS=$(echo ${VERSIONS} | jq '.DeleteMarkers[]')
fi
fi
info "Deleting versions..."
for VER in $(echo ${VERS} | jq -r '@base64')
do
N=$(echo ${VER} | base64 --decode | jq -r '.Key')
V=$(echo ${VER} | base64 --decode | jq -r '.VersionId')
debug "aws s3api delete-object --bucket ${BUCKET} --key ${N} --version-id ${V}"
out=$(aws s3api delete-object --bucket ${BUCKET} --key ${N} --version-id ${V})
info "Deleted: $(echo ${out} | jq -c '.')"
done
info "Done. Deleting markers..."
for DEL in $(echo ${DELS} | jq -r '@base64')
do
N=$(echo ${DEL} | base64 --decode | jq -r '.Key')
V=$(echo ${DEL} | base64 --decode | jq -r '.VersionId')
debug "aws s3api delete-object --bucket ${BUCKET} --key ${N} --version-id ${V}"
out=$(aws s3api delete-object --bucket ${BUCKET} --key ${N} --version-id ${V})
info "Deleted: $(echo ${out} | jq -c '.')"
done
debug "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment