lengthy_preamble
lengthy_preamble

Reputation: 434

Using boto to delete all buckets

I've been tasked with creating a script to delete all the current S3 buckets and create some new ones. This is something that they want to do on an ongoing basis. So far I have all the preliminaries:

import boto
from boto.s3.key import Key
import boto.s3.connection
from __future__ import print_function

conn = boto.s3.connect_to_region('us-east-1',
                                 aws_access_key_id='my_access_key', aws_secret_access_key='my_secret_key')

ls = conn.get_all_buckets()

print(*ls,sep='\n')

This gives me a list of all the current buckets. Now if I want to remove the buckets my understanding is that they have to be emptied first, using a method something like:

db = conn.get_bucket('bucket_name')
for key in db.list():
  key.delete()

And then I could do:

conn.delete_bucket('bucket_name')

I want to set it up such that it pulls each bucket name from 'ls', but I'm not sure how to go about this. I tried this:

for i in ls:
    db = conn.get_bucket('i')
    for key in db.list():
        key.delete()

But I get an error "S3ResponseError: 400 Bad Request". I'm getting a sneaking suspicion that it's not pulling the separate elements from the list. Do I maybe have to get data frames involved? As far as I know, boto doesn't have an option to just nuke all the folders outright.

Upvotes: 2

Views: 4080

Answers (1)

maafk
maafk

Reputation: 6896

I'd recommend using boto3

The following should do the trick, though it's untested (I don't want to delete all my buckets :))

import boto3

client = session.client('s3')
s3 = boto3.resource('s3')

buckets = client.list_buckets()

for bucket in buckets['Buckets']:
    s3_bucket = s3.Bucket(bucket['Name'])
    s3_bucket.objects.all().delete()
    s3_bucket.delete()

Upvotes: 6

Related Questions