Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active October 5, 2021 22:57
Delete AWS S3 bucket versioned objects and delete markers.

Delete S3 versioned objects and delete markers

Quick and dirty Bash script to iterate an S3 bucket and remove all object versions and delete markers.

Probably only usable on buckets with a few hundred version objects maximum, otherwise it might be more efficient and time effective to use a lifecycle rule to age out and remove objects in bulk.

Reference

#!/bin/bash -e
BUCKET_NAME="bucket-name"
function main {
local IFS=
local versionKeyRegexp=$'^([^\t]+)\t(.+)'
while read -r item; do
if [[ $item =~ $versionKeyRegexp ]]; then
local versionId=${BASH_REMATCH[1]}
local key=${BASH_REMATCH[2]}
aws s3api delete-object \
--bucket "$BUCKET_NAME" \
--key "$key" \
--version-id "$versionId"
echo "Deleted: [$key] | [$versionId]"
fi
done < <(aws s3api list-object-versions \
--bucket "$BUCKET_NAME" \
--output text \
--query "[DeleteMarkers,Versions][].{a:VersionId,b:Key}")
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment