Home > database >  Remove S3 obsolete lifecycle rule
Remove S3 obsolete lifecycle rule

Time:01-03

S3 usage current scenario

  1. General files stored without date of expiration.
  2. Archive file: stored in archive folder(expiration date set of the folder) with the date of expiration of 30 days.

Now there is an option to increase the date of retention of some archive files by n days. Example: Archive file-1 - retention day increased to be 5 more days. So, now I have to move the file out of the archive folder and create a separate expiration date for file-1. But the issue will be, when a file-1 gets deleted from s3, its associated lifecycle still remains present.

So, I want to remove the unused lifecycle. Is there any way to identify obsolete lifecycle?

CodePudding user response:

Instead of using S3 Lifecycle rules, I would recommend:

  • Store the desired deletion date in a Tag on the object to identify when you would like the object deleted
  • Trigger an AWS Lambda function once per day, which would:
    • List the contents of the bucket (or path within the bucket)
    • Check the date on the Tag of each object
    • If it has passed the expiration date, then Delete the object

Once you pass a few thousand objects, this process might not be efficient because you would need to call GetObjectTagging for each object (because listing the bucket unfortunately does not provide tags). It would then be more worthwhile to keep a database of object names and desired expiration dates, then have the Lambda function consult the database to identify which objects to delete.

  • Related