Search code examples
amazon-web-servicesamazon-s3versioningbucket

aws cloudformation snippet for S3 Bucket to apply LifecycleConfiguration to delete all existing versions


The lifecycle configuration yaml template snippet must delete all existing previous versions as well as current versions from now on. The versioning is suspended for this bucket. Any help is highly appreciated.


Solution

  • Using Boto3:

    #!/usr/bin/env python
    
    import boto3
    
    
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('your-bucket-name')
    bucket.object_versions.all().delete()
    

    You can do it on GUI too but that would take lots of time if you have lots of files.

    You can find further possible ways here.