How do I empty an Amazon S3 bucket using a lifecycle configuration rule?

Last updated: 2020-09-28

I have an Amazon Simple Storage Service (Amazon S3) bucket that stores millions of objects. I want to empty the bucket so that I won't be charged for storage anymore. How can I empty the bucket using a lifecycle configuration rule?

Resolution

Follow these steps to create a lifecycle rule that expires current versions of objects and permanently delete previous versions of objects:

  1. Open the Amazon S3 console.
  2. From the list of buckets, choose the bucket that you want to empty.
  3. Choose the Management tab.
  4. Choose Add lifecycle rule.
  5. Enter a name for the lifecycle rule.
  6. For Choose a rule scope, select Apply to all objects in the bucket. Then, choose Next.
  7. For Storage class transition, don't select any options. Then, choose Next. Because you're emptying the bucket, you don't need to transition objects to another storage class.
  8. For Configure expiration, enter the following:
    Select both Current version and Previous versions.
    For Expire current version of object, enter 1 to specify After 1 days from object creation.
    For Permanently delete previous versions, enter 1 to specify After 1 days from becoming a previous version.
    Select Clean up incomplete multipart uploads.
    Then, enter 1 to specify After 1 days from start of upload.
  9. Choose Next.
  10. Review the message under This rule applies to all objects in the bucket. Then, select I acknowledge that this lifecycle rule will apply to all objects in the bucket.
  11. Choose Save.

Amazon S3 runs lifecycle rules once every day. After the first time Amazon S3 runs the rules, all objects eligible for expiration are marked for deletion and you're no longer be charged for those objects. It can take a few days for the rules to run until the bucket is completely empty. This is because expiring object versions and cleaning up delete markers are asynchronous steps. For more information, see Understanding object expiration.