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

前端 未结 20 987
我在风中等你
我在风中等你 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:03

    I found the other answers either incomplete or requiring external dependencies to be installed (like boto), so here is one that is inspired by those but goes a little deeper.

    As documented in Working with Delete Markers, before a versioned bucket can be removed, all its versions must be completely deleted, which is a 2-step process:

    1. "delete" all version objects in the bucket, which marks them as deleted but does not actually delete them
    2. complete the deletion by deleting all the deletion marker objects

    Here is the pure CLI solution that worked for me (inspired by the other answers):

    #!/usr/bin/env bash
    
    bucket_name=...
    
    del_s3_bucket_obj()
    {
        local bucket_name=$1
        local obj_type=$2
        local query="{Objects: $obj_type[].{Key:Key,VersionId:VersionId}}"
        local s3_objects=$(aws s3api list-object-versions --bucket ${bucket_name} --output=json --query="$query")
        if ! (echo $s3_objects | grep -q '"Objects": null'); then
            aws s3api delete-objects --bucket "${bucket_name}" --delete "$s3_objects"
        fi
    }
    
    del_s3_bucket_obj ${bucket_name} 'Versions'
    del_s3_bucket_obj ${bucket_name} 'DeleteMarkers'
    

    Once this is done, the following will work:

    aws s3 rb "s3://${bucket_name}"
    

    Not sure how it will fare with 1000+ objects though, if anyone can report that would be awesome.

提交回复
热议问题