12

When I try to delete a bucket using the lines:

conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

print conn.delete_Bucket('BucketNameHere').message

It tells me the bucket I tried to delete is not empty.

The bucket has no keys in it. But it does have versions.

How can I delete the versions?

I can see the list of versions using bucket.list_versions()

Java has a deleteVersion Method on its s3 connection. I found that code here:

http://bytecoded.blogspot.com/2011/01/recursive-delete-utility-for-version.html

He does this line to delete the version:

s3.deleteVersion(new DeleteVersionRequest(bucketName, keyName, versionId));

Is there anything comparable in boto?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ChickenFur
  • 2,360
  • 3
  • 20
  • 26

1 Answers1

22

Boto does support versioned buckets after version 1.9c. Here's how it works:

import boto

s3 = boto.connect_s3()

#Create a versioned bucket
bucket = s3.create_bucket("versioned.example.com")
bucket.configure_versioning(True)

#Create a new key and make a few versions
key = bucket.new_key("versioned_object")
key.set_contents_from_string("Version 1")
key.set_contents_from_string("Version 2")

#Try to delete bucket
bucket.delete()   ## FAILS with 409 Conflict

#Delete our key then try to delete our bucket again
bucket.delete_key("versioned_object")
bucket.delete()   ## STILL FAILS with 409 Conflict

#Let's see what's in there
list(bucket.list())   ## Returns empty list []

#What's in there including versions?
list(bucket.list_versions())   ## Returns list of keys and delete markers

#This time delete all versions including delete markers
for version in bucket.list_versions():
    #NOTE we're still using bucket.delete, we're just adding the version_id parameter
    bucket.delete_key(version.name, version_id = version.version_id)

#Now what's in there
list(bucket.list_versions())   ## Returns empty list []

#Ok, now delete the bucket
bucket.delete()   ## SUCCESS!!
Capt. Crunch
  • 4,490
  • 6
  • 32
  • 38
secretmike
  • 9,814
  • 3
  • 33
  • 38
  • Just saved me a heap of hassle. Was driving me mad ! – Sirex Aug 24 '12 at 01:07
  • 1
    You should use delete_keys, not delete_key. it's super duper faster. See this for the equivalent solution, but using delete_keys: http://stackoverflow.com/questions/29809105/how-do-i-delete-a-versioned-bucket-in-aws-s3-using-the-cli – grayaii Mar 18 '16 at 23:44
  • @grayaii Good tip! – secretmike Mar 24 '16 at 15:27