BusterTheFatCat
BusterTheFatCat

Reputation: 57

deleting/replacing buckets in boto3

Full disclosure, this is for a boot camp, but I am pretty sure my code is correct and I have reached out several times to the admins for guidance but haven't gotten a response. I keep getting an error where it says (Bucket=bucket['Name'])..some error about the preset. I thought maybe I should replace Bucket with Prefix but it doesn't seem to be the case.

The idea here is to delete gim buckets and add the gid buckets in its place.

# Get the list_buckets response
response = s3.list_buckets()

# Delete all the buckets with 'gim', create replacements.
for bucket in response['Buckets']:
  if 'gim' in bucket['Name']:
      s3.delete_bucket(Bucket=bucket['Name'])

s3.create_bucket(Bucket='gid-staging')
s3.create_bucket(Bucket='gid-processed')

# Print bucket listing after deletion
response = s3.list_buckets()
for bucket in response['Bucket']:
    print(bucket['Name'])

Upvotes: 0

Views: 200

Answers (2)

Calvin
Calvin

Reputation: 131

Going off of what alex067 said, you can remove all objects and then delete the bucket like this:

for bucket in response['Buckets']:
  if 'gim' in bucket['Name']:
    bucket_resource = s3.Bucket(bucket['Name'])
    bucket_resource.objects.all().delete()
    res = bucket_resource.delete()

I would recommend setting this up to be able to run in a "dry" mode so as not to delete any unnecessary resources if something were to go wrong. Here's how that could work:

for bucket in response['Buckets']:
  if 'gim' in bucket['Name']:
    bucket_resource = s3.Bucket(bucket['Name'])
    print(bucket_resource.objects.all())

Upvotes: 3

alex067
alex067

Reputation: 3281

for bucket in response['Buckets']:
  if 'gim' in bucket['Name']:
      res = bucket.delete()

The bucket does have to be empty before you can delete it though.

Upvotes: 2

Related Questions