I've inherited an AWS account with a lot of S3 buckets that need to go. They're all nonempty, and some have versioning enabled. Is there a practical way to remove them in bulk without entering every bucket name by hand?
How can I delete a lot of nonempty S3 buckets without doing them one by one?
Answers
Use the AWS CLI to list the buckets and feed the names into a script. You don't need to type them all in. Have the script print the target list first, with an explicit exclusion for anything you're keeping, before it deletes anything.
For unversioned buckets, a recursive object delete followed by a bucket delete can do the job. Don't rely on that for versioned buckets, though. A recursive delete only removes the current objects, leaving old versions and delete markers behind.
Reply to this answer
For versioned buckets, use list-object-versions and delete-objects to remove both object versions and delete markers, then delete the bucket. Make sure the script handles pagination and checks for errors rather than assuming every delete succeeded.
The same approach matters if versioning is suspended. Existing versions can still be there, and the bucket has to be empty before S3 will remove it.
Reply to this answer
If this doesn't need to happen immediately, lifecycle rules are another option. Configure expiration for current and noncurrent versions and cleanup of expired delete markers, then check back before deleting the buckets. Lifecycle processing is asynchronous, so don't expect the buckets to empty right away.
Reply to this answer
If there's any doubt about the contents, consider restricting access first and allowing time for owners to raise concerns. You could also preserve anything needed elsewhere before cleanup. That's slower than a bulk delete, but it gives you a chance to catch a dependency before the data is gone.
Reply to this answer
Add your answer
Answers are reviewed before publication.
6m!zindle_493:
Also check for Object Lock or other retention restrictions. Those can prevent deletion even when the script has the usual permissions.