Tech Dreams

Automatically Delete Amazon S3 Files By Setting Expiration Time

Amazon S3 is a cost effective cloud storage service that lets you easily build application for cloud. Today a new feature called Object Expiration is added to Amazon S3 to allow automatic deletion of expired objects.

Consider a scenario where you want to build an application that stores log files on Amazon S3 storage service and the logs that are older than 30 days needs to be discarded. To delete expired logs you had to implement an in-house solution that regularly scans the list of S3 objects, identify objects created prior 30 days and delete them. This involves pretty good amount programming or scripting depending on your solution approach.

With Object Expiration feature, Amazon S3 automatically deletes the expired objects and all you need to do is to set a rule that specifies max age for identifying expired objects.

You can define Object Expiration rules for a set of objects in your bucket. Each expiration rule allows you to specify a prefix and an expiration period in days. The prefix field (e.g. “logs/”) identifies the object(s) subject to the expiration rule, and the expiration period specifies the number of days from creation date (i.e. age) after which object(s) should be removed.

Once the objects are past their expiration date, they will be queued for deletion.

To use Object Expiration from the AWS Management console:

  1. Under the Amazon S3 tab, select the bucket on which you want to apply Object Expiration rules.
  2. Select the “Properties” action on that bucket.
  3. Select the “Lifecycle” Tab.
  4. Under the “Lifecycle” tab, add an Object Expiration rule by specifying a prefix (e.g. “logs/”) that matches the object(s) you want to expire. Also specify the number of days from creation after which object(s) matching the prefix should be expired.
  5. You can optionally specify a name for the rule for better organization.

For a programmers guide on Object Expiration check this link of Amazon S3 documentation.