Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StrandedKitty/39d62b84cb52eaeae652361bfac7885d to your computer and use it in GitHub Desktop.
Save StrandedKitty/39d62b84cb52eaeae652361bfac7885d to your computer and use it in GitHub Desktop.
Delete all outdated versions of an object in S3
#!/bin/bash
S3_ENDPOINT="https://ams3.digitaloceanspaces.com"
S3_BUCKET="osm-tiles"
S3_OBJECT_KEY="vector/planet.mbtiles"
# Retrieve the list of all versions of the S3 object
versions=$(aws s3api list-object-versions --endpoint $S3_ENDPOINT --bucket $S3_BUCKET --prefix $S3_OBJECT_KEY --query 'Versions[].VersionId' --output text)
# Get the latest version ID
latest_version=$(aws s3api list-object-versions --endpoint $S3_ENDPOINT --bucket $S3_BUCKET --prefix $S3_OBJECT_KEY --query 'Versions[?IsLatest].[VersionId]' --output text)
# Iterate over each version and delete it (except the latest version)
for version in $versions; do
if [[ "$version" != "$latest_version" ]]; then
echo "Deleting version: $version"
aws s3api delete-object --endpoint $S3_ENDPOINT --bucket $S3_BUCKET --key $S3_OBJECT_KEY --version-id $version
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment