Skip to content

Lifecycle Policies

Expire noncurrent object versions automatically

A lifecycle policy deletes noncurrent versions of objects after a period you choose. Use one to keep the history that object versioning provides without storing every version forever.

A policy applies to file buckets and has no effect until you turn on versioning for the bucket.

Set a policy

To set a policy, call updateBucketLifecycle with a list of rules.

const { , } = await ..('avatars', {
: [
{
: 'expire-history',
: 'Enabled',
: {},
: {
: 30,
: 2,
},
},
],
})

This rule keeps the 2 newest noncurrent versions of each object. It expires the others after they've been noncurrent for 30 days. Both conditions in a rule must be true for a version to expire. If a policy has more than one rule, a version expires when any enabled rule matches it.

Noncurrent delete markers count toward newerNoncurrentVersions and expire like other noncurrent versions. Storage applies lifecycle rules about once a day, so a version can be deleted up to a day after it meets a rule's conditions.

Get or delete a policy

To read the policy, call getBucketLifecycle. If the bucket has no policy, the call fails with NoSuchLifecycleConfiguration.

To remove the policy, call deleteBucketLifecycle.

const { , } = await ..('avatars')
await ..('avatars')

Rule options

A policy holds 1 to 1,000 rules. Each rule has the following fields.

FieldTypeRequiredDescription
idStringNoA unique name for the rule, up to 255 characters. Storage generates one if you omit it.
statusStringYesEnabled runs the rule. Disabled keeps the rule stored without running it.
filterObjectYesMust be {}. A rule applies to every object in the bucket.
noncurrentVersionExpirationObjectYesHolds noncurrentDays and newerNoncurrentVersions.
noncurrentDaysIntegerYesThe number of days a version must be noncurrent before it expires. Must be 1 or more.
newerNoncurrentVersionsIntegerNoThe number of the newest noncurrent versions to keep whatever their age. Accepts 1 to 100.

Storage rejects any other filter value. Prefix, tag, and size filters aren't supported.

Expiring noncurrent versions is the only action a rule supports.