All Products
Search
Document Center

Object Storage Service:Lifecycle (Ruby SDK)

Last Updated:Mar 20, 2026

Use lifecycle rules to automatically transition infrequently accessed objects to lower-cost storage classes, or delete objects that are no longer needed. Rules trigger based on each object's last modified time, so you can apply different retention policies to different object prefixes.

Prerequisites

Before you begin, make sure that you have:

Usage notes

  • The examples use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For supported regions and endpoints, see Regions and endpoints.

  • The examples create an OSSClient instance using an OSS endpoint. To create an instance using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

Set lifecycle rules for a bucket

The following example sets two lifecycle rules on examplebucket:

  • rule1: applies to objects under the foo/ prefix and expires them after 3 days

  • rule2: applies to objects under the bar/ prefix and expires them on January 1, 2016 (disabled)

To update existing rules, see How do I change the configurations of one or more lifecycle rules?

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Replace with your actual endpoint.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Read credentials from environment variables to avoid hardcoding sensitive values.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

bucket = client.get_bucket('examplebucket')

bucket.lifecycle = [
  # Rule 1: delete objects under foo/ that are older than 3 days.
  Aliyun::OSS::LifeCycleRule.new(
    id: 'rule1', enable: true, prefix: 'foo/', expiry: 3),

  # Rule 2: delete objects under bar/ on a specific date (rule is disabled).
  Aliyun::OSS::LifeCycleRule.new(
    id: 'rule2', enable: false, prefix: 'bar/', expiry: Date.new(2016, 1, 1))
]

The LifeCycleRule parameters are described below:

ParameterTypeDescription
idStringUnique identifier for the rule within the bucket
enableBooleanWhether the rule is active. Set to false to disable without deleting the rule
prefixStringObject prefix the rule applies to. Use an empty string to match all objects
expiryInteger or DateDays after last modification (Integer), or a specific expiration date (Date object)

Get lifecycle rules for a bucket

The following example retrieves all lifecycle rules configured on examplebucket:

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Replace with your actual endpoint.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Read credentials from environment variables to avoid hardcoding sensitive values.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

bucket = client.get_bucket('examplebucket')

# Retrieve and print all lifecycle rules.
rules = bucket.lifecycle
puts rules

Delete all lifecycle rules for a bucket

Assigning an empty array to bucket.lifecycle removes all lifecycle rules from the bucket.

To delete individual rules instead of all rules, see How do I delete one or more lifecycle rules?

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Replace with your actual endpoint.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Read credentials from environment variables to avoid hardcoding sensitive values.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

bucket = client.get_bucket('examplebucket')

# Delete all lifecycle rules by assigning an empty array.
bucket.lifecycle = []

References