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

Last updated: 2022-11-01

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?

Short description

Based on the type of versioning that you're using for your bucket, different lifecycle configuration rules are required to delete all objects in the bucket.

For buckets with versioning turned off, expire the current versions of the objects, and then permanently delete any incomplete multipart uploads.

For buckets with versioning turned on and versioning-suspended buckets, you must create two lifecycle rules:

  1. Expire the current versions of the objects and then permanently delete the non-current versions.
  2. Expire any lone delete markers present in the bucket, and then permanently delete any incomplete multipart uploads.

In this case, separate rules are needed because the Delete expired object delete markers action won't complete when Expire current versions of objects is selected.

Based on your configuration, see the related Resolution section of this article.

Note: Amazon S3 runs lifecycle rules once every day. After the first time that Amazon S3 runs the rules, all objects that are eligible for expiration are marked for deletion. You're no longer charged for objects that are marked for deletion. However, expiring object versions and removing up markers are asynchronous steps, and may take several days to complete. For more information about asynchronous object removal in Amazon S3, see Expiring objects.

Resolution

Buckets with versioning turned on and versioning-suspended buckets

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 Create lifecycle rule.

5.    For Lifecycle rule name, enter a rule name.

6.    For Choose a rule scope, select Apply to all objects in the bucket.

7.    Select I acknowledge that this rule will apply to all objects in the bucket.

8.    For Lifecycle rule actions, select the following to create a lifecycle rule:
        Expire current versions of objects
        Permanently delete noncurrent versions of objects

9.    In the Expire current versions of objects field, enter "1" in the Days after object creation field.

10.    In the Permanently delete noncurrent versions of objects field, enter "1" in the Days after objects become noncurrent field.

11.    Leave the Number of newer versions to retain field empty to delete all versions.

13.    Choose Create rule.

14.    Create a second lifecycle rule by repeating steps 4-7.

15.    For Lifecycle rule actions, select Delete expired object delete markers or incomplete multipart uploads

16.    Select Delete expired object delete markers.

17.    Select Delete incomplete multipart uploads. Then, enter 1 in the Number of days field.

17.    Choose Create rule.

Buckets with versioning turned off

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 Create lifecycle rule.

5.    For Lifecycle rule name, enter a rule name.

6.    For Choose a rule scope, select Apply to all objects in the bucket.

7.    Select I acknowledge that this rule will apply to all objects in the bucket.

8.    For Lifecycle rule actions, select the following to create a lifecycle rule:

        Expire current versions of objects
        Delete expired object delete markers or incomplete multipart uploads

9.    In the Expire current versions of objects field, enter "1" in the Days after object creation field.

10.    In the Delete expired object delete markers or incomplete multipart uploads field, select Delete incomplete multipart uploads. Then, enter 1 for the Number of days field.

11.    Choose Create rule.