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

前端 未结 20 933
我在风中等你
我在风中等你 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 16:50
    1. For deleting specify object(s), using jq filter.
    2. You may need cleanup the 'DeleteMarkers' not just 'Versions'.
    3. Using $() instead of ``, you may embed variables for bucket-name and key-value.
    aws s3api delete-objects --bucket bucket-name --delete "$(aws s3api list-object-versions --bucket bucket-name | jq -M '{Objects: [.["Versions","DeleteMarkers"][]|select(.Key == "key-value")| {Key:.Key, VersionId : .VersionId}], Quiet: false}')"
    
    0 讨论(0)
  • 2020-12-07 16:55

    I ran into the same limitation of the AWS CLI. I found the easiest solution to be to use Python and boto3:

    #!/usr/bin/env python
    
    BUCKET = 'your-bucket-here'
    
    import boto3
    
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(BUCKET)
    bucket.object_versions.delete()
    
    # if you want to delete the now-empty bucket as well, uncomment this line:
    #bucket.delete()
    

    A previous version of this answer used boto but that solution had performance issues with large numbers of keys as Chuckles pointed out.

    0 讨论(0)
  • 2020-12-07 16:56

    To add to python solutions provided here: if you are getting boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request error, try creating ~/.boto file with the following data:

    [Credentials]
    aws_access_key_id = aws_access_key_id
    aws_secret_access_key = aws_secret_access_key
    [s3]
    host=s3.eu-central-1.amazonaws.com
    aws_access_key_id = aws_access_key_id
    aws_secret_access_key = aws_secret_access_key
    

    Helped me to delete bucket in Frankfurt region.

    Original answer: https://stackoverflow.com/a/41200567/2586441

    0 讨论(0)
  • 2020-12-07 16:59

    If you want pure CLI approach (with jq):

    aws s3api list-object-versions \
              --bucket $bucket \
              --region $region \
              --query "Versions[].Key"  \
              --output json | jq 'unique' | jq -r '.[]' | while read key; do
       echo "deleting versions of $key"
       aws s3api list-object-versions \
              --bucket $bucket \
              --region $region \
              --prefix $key \
              --query "Versions[].VersionId"  \
              --output json | jq 'unique' | jq -r '.[]' | while read version; do
         echo "deleting $version"
         aws s3api delete-object \
              --bucket $bucket \
              --key $key \
              --version-id $version \
              --region $region
       done
    done          
    
    0 讨论(0)
  • 2020-12-07 17:00

    Looks like as of now, there is an Empty button in the AWS S3 console.

    Just select your bucket and click on it. It will ask you to confirm your decision by typing permanently delete Note, this will not delete the bucket itself.

    0 讨论(0)
  • 2020-12-07 17:01

    Even though technically it's not AWS CLI, I'd recommend using AWS Tools for Powershell for this task. Then you can use the simple command as below:

    Remove-S3Bucket -BucketName {bucket-name} -DeleteBucketContent -Force -Region {region}
    

    As stated in the documentation, DeleteBucketContent flag does the following:

    "If set, all remaining objects and/or object versions in the bucket are deleted proir (sic) to the bucket itself being deleted"

    Reference: https://docs.aws.amazon.com/powershell/latest/reference/

    0 讨论(0)
提交回复
热议问题