Skip to content

Instantly share code, notes, and snippets.

@nick-stocks
Forked from magnetikonline/README.md
Created July 22, 2021 08:49
Show Gist options
  • Save nick-stocks/0103149815ca6fc6b94b2a09d9c3abe6 to your computer and use it in GitHub Desktop.
Save nick-stocks/0103149815ca6fc6b94b2a09d9c3abe6 to your computer and use it in GitHub Desktop.
Delete all AWS S3 bucket versioned objects and delete markers.

Delete all 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