Deleting Buckets
To remove all the buckets and objects you have created, you must first make sure that your buckets have no objects within them. You must first delete every single object within the bucket, or else the BucketNotEmpty
exception will be raised. When you have a versioned bucket, you need to delete every object and all its versions.
00:00
Before you can delete a bucket you need to delete all the objects within that bucket. If you try to delete the bucket anyway, you’ll get a BucketNotEmpty
error raised when you run your code. Additionally, for version buckets, all versions of every object must be deleted.
00:17
Let’s go ahead and define a function that will programmatically delete every object within a bucket. So let’s define delete_all_objects()
. And in here, you’re going to pass an s3_connection
and the bucket_name
.
00:35
All right, define a list and we’ll just call that something like res
(result). And then the bucket
is going to equal your s3_connection
and call .Bucket()
off of that and pass in the bucket_name
.
00:53
Now make a loop, so for obj_version in bucket.object_versions.all():
you’re going to append that to res
and you’ll pass in the 'Key'
, which will just be the obj_version.key
,
01:26
and the 'VersionId'
, which is just obj_version.id
. And once this loop is done for us, we can go ahead and print that res
, and then with the bucket
, go ahead and .delete_objects()
and pass in Delete
,
01:52
and then within here, you’re going to add a dictionary which will just be 'Objects'
and pass in that res
. There’s a couple of things going on here.
02:01 What this is looping through is all versions of all objects. Now, if you don’t have versioning enabled in the bucket, the version of these objects will be null, so it’ll still work.
02:14
The other thing that you may notice is that we’re not creating a new Object
instance for every object in the bucket, but all of the key and version information is being stored in this res
list, which is then being passed into .delete_objects()
.
02:30 This is because you can batch up to 1,000 deletions in a single API call, so it’s good to group them as much as possible to save costs as AWS charges based on the number of API calls that you make.
02:44
So, let’s save this and open up a terminal. Get into your Python interpreter, import boto3
, and then from boto3_guide import delete_all_objects
.
03:03
Let’s make that s3_resource
that we’ve been using, which is equal to boto3.resource('s3')
, and then grab that first_bucket_name
,
03:17 which I have saved over here.
03:22
Copy that, paste. Cool. Let’s try out this new function! delete_all_objects()
, pass in the s3_resource
and the first_bucket_name
.
03:42
Aha, what do we have here? So, this is a pretty good typo, because I am great at not typing things in correctly. I have misspelled version
.
03:58
There we go. Let me make sure that’s correct too. Okay. That looks like that was the only bad one. Let’s open up a new terminal so that we can load that new corrected version in here. Grab the Python interpreter, import boto3
, from boto3_guide import delete_all_objects
.
04:31
Make that new s3_resource
04:40
and the first_bucket_name
.
04:52
All right, let’s try that again. delete_all_objects(s3_resource, first_bucket_name)
. Cool. That looks like much better output, so let’s take a look at it.
05:12
All right, cool. So we can see that there’s a list here and we’ve got a number of keys and version IDs. Now, there were a couple of files that were added before we enabled versioning in this bucket, so you’ll see a 'VersionId': 'null'
, but then for some of the later ones, we have version IDs here.
05:31 So seeing this and not seeing any errors, you can now know that everything that was in that first bucket is now gone. And even though we see that the function worked with null versions, let’s go ahead and try this on the second bucket where versioning was never enabled.
05:53
So, make a second_bucket_name
, grab the second bucket, paste that in, and run the delete_all_objects()
again, passing your s3_resource
and the second_bucket_name
.
06:18 Cool. And in here, this was just that first file that was copied over, and there weren’t any version IDs in here. But you can see that there were still no errors, and this bucket is now empty.
06:29
Now that both buckets are empty, it’s time to delete them. You’ll see that this is pretty similar to deleting an object, where you’re going to make a new instance, which will just be a Bucket()
, and pass in the first_bucket_name
.
06:50
And from here, you can just call .delete()
. Alrighty! Look at that. You’ve got some response text here with an 'HTTPStatusCode'
of 204
.
07:03
Perfect. Let’s go ahead and do that with the second bucket. So we can just call the exact same thing here, second_bucket_name
and the .delete()
method.
07:16
And look at that—another 204
status code, and that bucket is gone. And I guess we could take a look at this by going to my S3 under this user.
07:28 I see some buckets here. Let me refresh this. And look at that! There are no buckets left. Here is how to get started. Cool! So both of those buckets have been deleted. Congratulations!
07:44 You’ve covered some pretty powerful topics with regard to Boto3 and S3, and you should feel comfortable with programmatically interacting with S3. In the next video, we’ll briefly discuss Infrastructure as Code, or IaC, and how it can work together with Boto3 for managing your S3 assets. Thanks for watching.
Become a Member to join the conversation.
alvarodelvalle2 on Dec. 9, 2020
What are you using to support code completion with boto3? Great Course BTW!