LifeCycle vs Retention Policy

Harshit Sharma - Sep 18 - - Dev Community

Feb 12, 2024 Originally published at Medium

I wonder 🤔 sometimes that why Google decided to have different policy/rule for a similar purpose, When I started working on GCS buckets I was very confused about these two things.

So I am here to simplify things for you…

1. Retention policy:

In simple words Retention policy is basically a policy or rule which implies on the bucket, so the objects stored in the bucket will not be deleted or modified till the retention period ends.

NOTE: Retention period means the minimum time the data will be stored in the bucket(Can’t modified/deleted)

Retention time periods can be set in seconds(s), days(d), months(m), years(y).

Example: If we have retention period of 5 days on a bucket then the objects stored in the bucket will not be deleted or modified till 5 days from object stored in the bucket.

GCS Bucket with retention policy enabled for 5 days

However, there is a twist; even though we have applied a retention policy to the bucket, it has not been locked yet. According to the retention policy, we cannot modify or delete the objects in the bucket. Nevertheless, we still have the ability to remove the policy and delete the bucket directly, resulting in the loss of data stored in the bucket.

So here lock 🔒 comes in the picture….

If we apply a lock on the bucket to which a retention policy is applied, it means that the bucket cannot be deleted, and the retention policy’s time period cannot be reduced until the objects stored in the bucket surpass the specified retention time period.

NOTE: Retention policy time period can be increased when lock is applied but it can’t be reduced.

Example: If we set a retention period of 5 days and also apply a lock to the bucket, we are unable to remove the bucket and policy, and we cannot reduce the retention period to less than 5 days. However, it’s possible to increase the retention period to greater than 5 days if the lock is applied

2. LifeCycle:

Lifecycle is also similar to the retention policy, but it specifies that if we set a Lifecycle rule, then a specific action will be performed on the bucket when certain conditions are met.

Example:

lifecycle_rules = [
{
condition ={
age = 365
}
action = {
type = “Delete”
}
}
]

The condition states that if objects stored in the bucket have been present for 365 days, they will be automatically deleted after this period.

NOTE: In this scenario, objects stored in the bucket can be manually deleted before they meet the condition specified in the lifecycle policy. However, with the retention policy, objects cannot be deleted before the specified time period

If you want to check other available conditions then you can follow this document [https://cloud.google.com/storage/docs/lifecycle-configurations]

NOTE: We can work with both together for our purpose, it depends how we want it.

Example 1: If we apply both the lifeCycle Rule and Retention policy on the bucket.

Lifecycle Age => 30 days, Action => Delete

Retention Periods => 30 days

Then the object/data stored in the bucket automatically deleted after 30 days from the day object stored in the bucket.

Example 2: If we apply both the lifeCycle Rule and Retention policy on the bucket, but both are different.

Lifecycle Age => 30 days, Action => Delete

Retention Periods => 40 days

Then the object/data stored in the bucket automatically deleted after 40 days from the day object stored in the bucket. in this case Retention Period will take precedence over the LifeCycle rule.

Example 3: If we apply both the lifeCycle Rule and Retention policy on the bucket, but both are different.

Lifecycle Age => 40 days, Action => Delete

Retention Periods => 30 days

Then the object/data stored in the bucket automatically deleted after 40 days from the day object stored in the bucket. in this case LifeCycle rule will take precedence over the Retention Period.

Thanks 🙏 for reading out this blog Please do share if you like it.

Connect with me on Linkedin

.
Terabox Video Player