Skip to content

Instantly share code, notes, and snippets.

@seventhskye
Last active February 14, 2023 12:08
Show Gist options
  • Save seventhskye/0cc7b2804252975d36dca047ab7729e9 to your computer and use it in GitHub Desktop.
Save seventhskye/0cc7b2804252975d36dca047ab7729e9 to your computer and use it in GitHub Desktop.
A script to delete all objects, versions and delete markers from an s3 bucket.
#!/usr/bin/env python
import boto3
client = boto3.client('s3')
Bucket = 'a-bucket'
Prefix = 'a-prefix' # leave blank to delete the entire contents
IsTruncated = True
MaxKeys = 1000
KeyMarker = None
while IsTruncated == True:
if not KeyMarker:
version_list = client.list_object_versions(
Bucket=Bucket,
MaxKeys=MaxKeys,
Prefix=Prefix)
else:
version_list = client.list_object_versions(
Bucket=Bucket,
MaxKeys=MaxKeys,
Prefix=Prefix,
KeyMarker=KeyMarker)
try:
objects = []
versions = version_list['Versions']
for v in versions:
objects.append({'VersionId':v['VersionId'],'Key': v['Key']})
response = client.delete_objects(Bucket=Bucket,Delete={'Objects':objects})
print response
except:
pass
try:
objects = []
delete_markers = version_list['DeleteMarkers']
for d in delete_markers:
objects.append({'VersionId':d['VersionId'],'Key': d['Key']})
response = client.delete_objects(Bucket=Bucket,Delete={'Objects':objects})
print response
except:
pass
IsTruncated = version_list['IsTruncated']
KeyMarker = version_list['NextKeyMarker']
@giansmart
Copy link

giansmart commented Jan 31, 2023

Since the KeyMarker property is no longer available at this point, I changed and tested the above code as following.
Heads up: ChatGPT could not help me with this requirement.

#!/usr/bin/env python
import boto3

def delete_markers(bucket_name, prefix):
    client = boto3.client('s3')
    Bucket = bucket_name
    Prefix = prefix
    IsTruncated = True
    MaxKeys = 1000
    
    while IsTruncated == True:
        version_list = client.list_object_versions(
            Bucket=Bucket,
            MaxKeys=MaxKeys,
            Prefix=Prefix
        )
        try:
            objects = []
            versions = version_list ['Versions']
            for v in versions:
                objects.append({'VersionId':v['VersionId'],'Key':v['Key']})
                response = client.delete_objects(Bucket=Bucket,Delete={'Objects':objects})
                print (response)
        except:
            pass
        try:
            objects = []
            delete_markers = version_list['DeleteMarkers']
            for d in delete_markers:
                objects.append({'VersionId':d['VersionId'],'Key': d['Key']})
                response = client.delete_objects(Bucket=Bucket,Delete={'Objects':objects})
                print (response)
        except:
            pass
        IsTruncated = version_list['IsTruncated']

delete_markers('my_bucket_name','my/path')

@hemchander007
Copy link

Hi @giansmart ,

Thanks a lot above script worked smootly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment