/
Using boto3 to delete old object versions
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()
Related articles
, multiple selections available,
Related content
DataCore Swarm log4j Remediation (CVE-2021-44228)
DataCore Swarm log4j Remediation (CVE-2021-44228)
Read with this
Object Versioning
Object Versioning
More like this
Sample Java Code for using Content Gateway's S3 Interface
Sample Java Code for using Content Gateway's S3 Interface
Read with this
Bucket Lifecycle Policy
Bucket Lifecycle Policy
More like this
Using the Hadoop S3a connector with Gateway S3
Using the Hadoop S3a connector with Gateway S3
Read with this
Versioned buckets may have undue space usage
Versioned buckets may have undue space usage
More like this
© DataCore Software Corporation. · https://www.datacore.com · All rights reserved.