Using boto3 to delete old object versions

If you enable versioning in a bucket but then repeatedly update objects, old versions will accumulate and take up space. You can remove all old versions of objects, so that only the current live objects remain, with a script like below.

Just change the endpoint_url, bucket name, and prefix.

TODO: use argparse and make this a parameterized script, if such a tool does not already exist. Deletes should probably be batched. Error handling and retries might be necessary.

#!/usr/local/bin/python
import boto3 # requires "pip install boto3"

# Can enable some very verbose logging
import httplib
import logging
#httplib.HTTPConnection.debuglevel = 1
logging.basicConfig()
#logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

# List https://support.cloud.caringo.com/tmp?prefix=versiontest/&versions
session = boto3.Session()
s3 = session.resource(service_name='s3',
endpoint_url='https://support.cloud.caringo.com',
aws_access_key_id='71c3845cca47a56323eba7fbdb923ffb',
aws_secret_access_key='secret')
bucket = s3.Bucket('tmp')

for i in bucket.object_versions.filter(Prefix='versiontest/'):
print i, i.is_latest
if not i.is_latest:
print 'Deleting old version: ' + str(i)
i.delete()



© DataCore Software Corporation. · https://www.datacore.com · All rights reserved.