How to Empty Large S3 Buckets using S3 Lifecycle Policies

AWS S3 provides basically unlimited storage. Unfortunately, this storage doesn't come free. So If you don't clean up your s3 buckets you'll be paying a massive AWS bill just to store stuff you don't need.

If you want to delete an s3 bucket completely you need to empty the bucket first. Manually deleting old objects from a large bucket can be time-consuming. S3 lifecycle policies offer a solution to this problem by automatically transitioning objects to different storage classes or deleting them altogether based on their age or other criteria.

In this blog post, we'll show you how to use S3 lifecycle policies to empty large S3 buckets, helping you optimize your storage costs and ensure that your data is stored and retained according to your business needs.

What are S3 Lifecycle Policies?

S3 lifecycle policies are rules that you can define to manage the lifecycle of objects in your S3 buckets. You can use lifecycle policies to transition objects to different storage classes or delete them altogether based on their age or other criteria. Lifecycle policies can help you optimize your storage costs and ensure that your data is stored and retained according to your business needs.

How to Empty Large S3 Buckets using S3 Lifecycle Policies

To empty a large S3 bucket using S3 lifecycle policies, follow these steps:

  1. Open the S3 console and navigate to the bucket that you want to clean up.

  2. Click on the "Management" tab.

  3. Click on the "Create lifecycle rule" button.

  4. Give your rule a name.

  5. Choose a rule scope. e.g.: you can apply the rule to all objects or just a prefix in the bucket.

  6. Click on Expire current versions of objects and Permanently delete noncurrent versions of objects (for buckets with s3 versioning).

  7. Select after how many days the objects will be deleted.

  8. Click "Create rule" to activate it.

Your lifecycle rule will now automatically delete objects from your bucket that are older than the number of days you specified.

It's important to note that as of now, S3 lifecycle rules only run once a day. In fact, in one of my tests, even after 24 hours the objects weren't deleted yet. So, I thought that something must be wrong with my rule, but when I came back later the bucket was empty. This solution really helps when the bucket has lots of small files as deleting them using CLI is a huge pain. Of course, it's also useful if you have a lot of shortlived data that can be deleted soon too.

Conclusion

By using S3 lifecycle policies to automatically delete old objects from your S3 buckets, you can save time and effort while ensuring that your data is stored and retained according to your business needs. Whether you're cleaning up a large bucket or managing the lifecycle of objects in S3, S3 lifecycle policies can help you optimize your storage costs and keep your buckets organized and efficient.

Did you find this article valuable?

Support Laurynas Tumosa by becoming a sponsor. Any amount is appreciated!