How do I delete a versioned bucket in AWS S3 using the CLI?

前端 未结 20 942
我在风中等你
我在风中等你 2020-12-07 16:34

I have tried both s3cmd:

$ s3cmd -r -f -v del s3://my-versioned-bucket/

And the AWS CLI:

$ aws s3 rm s3://my-v         


        
20条回答
  •  遥遥无期
    2020-12-07 17:01

    This works for me. Maybe running later versions of something and above > 1000 items. been running a couple of million files now. However its still not finished after half a day and no means to validate in AWS GUI =/

    # Set bucket name to clearout
    BUCKET = 'bucket-to-clear'
    
    import boto3
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(BUCKET)
    
    max_len         = 1000      # max 1000 items at one req
    chunk_counter   = 0         # just to keep track
    keys            = []        # collect to delete
    
    # clear files
    def clearout():
        global bucket
        global chunk_counter
        global keys
        result = bucket.delete_objects(Delete=dict(Objects=keys))
    
        if result["ResponseMetadata"]["HTTPStatusCode"] != 200:
            print("Issue with response")
            print(result)
    
        chunk_counter += 1
        keys = []
        print(". {n} chunks so far".format(n=chunk_counter))
        return
    
    # start
    for key in bucket.object_versions.all():
        item = {'Key': key.object_key, 'VersionId': key.id}
        keys.append(item)
        if len(keys) >= max_len:
            clearout()
    
    # make sure last files are cleared as well
    if len(keys) > 0:
        clearout()
    
    print("")
    print("Done, {n} items deleted".format(n=chunk_counter*max_len))
    #bucket.delete() #as per usual uncomment if you're sure!
    

提交回复
热议问题