Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active October 5, 2021 22:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save magnetikonline/6f60e019467b6b0e4366c4d84e58495a to your computer and use it in GitHub Desktop.
Save magnetikonline/6f60e019467b6b0e4366c4d84e58495a to your computer and use it in GitHub Desktop.
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