Sometimes, you just need to delete all the objects from a versioned S3 bucket. Sadly, there is no built-in way to do this from the AWS CLI or even the web console!

Here’s a quick script I wrote in Python to do the job, using boto3:

import boto3

s3 = boto3.resource('s3')
bucket_name = input('Input name of bucket to clean: ').strip()
bucket = s3.Bucket(bucket_name)

# Read out full list of object keys and IDs (we'll store only non-duplicate keys, as 
# some keys may reoccur if there are multiple versions of an object present)
print('Listing objects...')
keys = []
for version in bucket.object_versions.all():
    if version.object_key not in keys:
        keys.append(version.object_key)

# Now, we walk through the list of keys deleting ALL versions
# of an object that we find
print('Deleting objects...')
for key in keys:
    bucket.object_versions.filter(Prefix=key).delete()

print('Done! All objects deleted from s3://{}'.format(bucket_name))

You can also download a copy from here.

The script isn’t perfect and could probably be optimized further, but it works well enough for buckets with just a few hundred or a few thousand objects. If you wanted to optimize, you could replace the keys array with a dictionary, or look deeper into the boto3 documentation to see if it’s possible to filter the list of objects on the server side.