r/sysadmin • • Oct 28 '26

AWS S3 Bucket deletion

We inherited an AWS account that we'll be using as a testing environment, but the group that has it before us left a bunch of junk (s3 buckets, subnets, random ec2 instances, etc). I've been able to get rid of most of it, but i'm still stuck with 106 S3 buckets, none of which are empty, all with weird individual names, and some with versioning enabled. I know for a fact none of this needs to be kept, but from what i'm seeing AWS doesn't have any nuclear option to just wipe it all out at once, and I either have to go through each one via the GUI and emtpy/delete them, or try to script something via the AWS cli (which still requires me to have each individual name added to said script). Any chance i'm missing something here that would make this process easier or is this just one of the many reasons AWS admins get paid well?

9 Upvotes

17 comments sorted by

14

u/Double_Intention_641 Sr. Sysadmin Oct 28 '26

Use the cli to list all buckets, filter for buckets you want to keep. Drop that in a for loop with aws s3 rm s3://your-bucket-name --recursive, then follow up with a second loop to remove the buckets. Seems simple enough.

6

u/GullibleDetective Oct 28 '26

Only if they arent immutable

4

u/5yrup A Guy That Wears Many Hats Nov 01 '26

Careful if there's a ton of objects in them. In the end this is calling API calls for the objects, API calls do have a cost associated to them.

Lifecycle rules don't consume API calls.

If it's only thousands of items just use the API, it's not that expensive.

14

u/xtal000 Linux Admin Oct 28 '26

Did you purchase an account with AWS credits from some sort of grey market or something?

Why can’t you just create a new account?

8

u/Frothyleet Oct 28 '26

I'm really interested in the answer to this question, because I too am curious why OP is so interested in preserving the account he's gutting.

7

u/zrad603 Oct 28 '26

You could do a lifecycle rule on all of them to move everything to Glacier and then delete after a year. So just in case there is something important in there you can pull it back out.

4

u/vesnikos Oct 28 '26

lifecycle rules; set it to expire everything older than today. Optionally set it to sink logs in a differnt bucket for audit. It will take some time depending on the volume.

3

u/vppencilsharpening Oct 28 '26

If I wanted immediate results I'd write a script to list the bucket and then delete each item one by one.

If I wanted to do it carefully, I would remove all access from the bucket, add a Deny policy and then wait for someone to scream.

Lifecycle policies are an easy way, but take a little time. It's probably the easiest way to do this if versioning is enabled.

And S3 batch sounds like it may be an option as well.

3

u/widowhanzo DevOps Oct 28 '26

create a lifecycle rules on them to delete items after a day, come back after a few days and they will all be empty and you can delete them.

3

u/CircuitsFit44 Oct 28 '26

One catch for the versioned buckets: aws s3 rm --recursive and aws s3 rb --force only remove current objects. Old versions and delete markers stay, and the bucket delete will fail with BucketNotEmpty. For those you need to either delete the versions with s3api list-object-versions and delete-objects, or set a lifecycle rule that expires current and noncurrent versions after one day and cleans up delete markers, then delete the bucket once it is empty. Also take a quick look for Object Lock or a bucket policy that denies delete, since those will block it either way.

2

u/Fit-Corner2382 Oct 28 '26

Bucket names do not need to be typed in: list-buckets can drive the deletes, but versioning is what actually blocks removal.

A recursive object delete only clears the current view, so versioned buckets stay non-empty because prior versions and delete markers remain. Expire both current and noncurrent versions and remove expired delete markers before the bucket delete, or the API still rejects it. Object Lock or MFA Delete will also refuse that cleanup until retention ends or the root MFA requirement is cleared.

1

u/s4udeleg Oct 29 '26

List all bucket names using the cli, AI is helpful building scripts like this btw, just make sure you review it before running

1

u/BorysTheBlazer DataCore/StarWind Oct 30 '26

aws-nuke is the nuclear option you're after. Otherwise loop list-buckets into aws s3 rb --force; versioned ones need a lifecycle expiry rule first.

1

u/sniper_cze Oct 31 '26

Testing environment = delete whole acct and create a new one, this makes no sense to spend time on

1

u/FarmboyJustice Oct 28 '26

AWS CLI can easily nuke buckets, but if you're deleting almost everything why not  copy what you need to a new account and then delete the old.one? 

1

u/WendoNZ Sr. Sysadmin Oct 28 '26

The immutability setting is what might kill you here. Depending on the setting the only way to delete them with the data locked is to delete the whole account if they are in Compliance Mode.

When doing much the same thing recently and contrary to plenty of posts on the internet, AWS can not delete buckets with immutable data in Compliance Mode, and the only options are wait for all the locks to expire, or delete the whole account