INTEGRITY Cloudflare Docs

Object lifecycles

Object lifecycles determine the retention period of objects uploaded to your bucket and allow you to specify when objects should transition from Standard storage to Infrequent Access storage.

A lifecycle configuration is a collection of lifecycle rules that define actions to apply to objects during their lifetime.

For example, you can create an object lifecycle rule to delete objects after 90 days, or you can set a rule to transition objects to Infrequent Access storage after 30 days.

Behavior

Configure lifecycle rules for your bucket

When you create an object lifecycle rule, you can specify which prefix you would like it to apply to.

Dashboard

  1. In the Cloudflare dashboard, go to the R2 object storage page.

    Go to Overview ↗
  2. Locate and select your bucket from the list.

  3. From the bucket page, select Settings.

  4. Under Object Lifecycle Rules, select Add rule.

  5. Fill out the fields for the new rule.

  6. When you are done, select Save changes.

Wrangler

  1. Install npm.
  2. Install Wrangler, the Developer Platform CLI.
  3. Log in to Wrangler with the wrangler login command.
  4. Add a lifecycle rule to your bucket by running the r2 bucket lifecycle add command.
npx wrangler r2 bucket lifecycle add <BUCKET_NAME> [OPTIONS]

Alternatively you can set the entire lifecycle configuration for a bucket from a JSON file using the r2 bucket lifecycle set command.

npx wrangler r2 bucket lifecycle set <BUCKET_NAME> --file <FILE_PATH>

The JSON file should be in the format of the request body of the put object lifecycle configuration API.

S3 API

Below is an example of configuring a lifecycle configuration (a collection of lifecycle rules) with different sets of rules for different potential use cases.

Configure the S3 client to interact with R2
const client = new S3({
	endpoint: "https://<account_id>.r2.cloudflarestorage.com",
	credentials: {
		accessKeyId: "<access_key_id>",
		secretAccessKey: "<access_key_secret>",
	},
	region: "auto",
});
Set the lifecycle configuration for a bucket
await client
	.putBucketLifecycleConfiguration({
		Bucket: "testBucket",
		LifecycleConfiguration: {
			Rules: [
				// Example: deleting objects on a specific date
				// Delete 2019 documents in 2024
				{
					ID: "Delete 2019 Documents",
					Status: "Enabled",
					Filter: {
						Prefix: "2019/",
					},
					Expiration: {
						Date: new Date("2024-01-01"),
					},
				},
				// Example: transitioning objects to Infrequent Access storage by age
				// Transition objects older than 30 days to Infrequent Access storage
				{
					ID: "Transition Objects To Infrequent Access",
					Status: "Enabled",
					Transitions: [
						{
							Days: 30,
							StorageClass: "STANDARD_IA",
						},
					],
				},
				// Example: deleting objects by age
				// Delete logs older than 90 days
				{
					ID: "Delete Old Logs",
					Status: "Enabled",
					Filter: {
						Prefix: "logs/",
					},
					Expiration: {
						Days: 90,
					},
				},
				// Example: abort all incomplete multipart uploads after a week
				{
					ID: "Abort Incomplete Multipart Uploads",
					Status: "Enabled",
					AbortIncompleteMultipartUpload: {
						DaysAfterInitiation: 7,
					},
				},
				// Example: abort user multipart uploads after a day
				{
					ID: "Abort User Incomplete Multipart Uploads",
					Status: "Enabled",
					Filter: {
						Prefix: "useruploads/",
					},
					AbortIncompleteMultipartUpload: {
						// For uploads matching the prefix, this rule will take precedence
						// over the one above due to its earlier expiration.
						DaysAfterInitiation: 1,
					},
				},
			],
		},
	})
	.promise();

Get lifecycle rules for your bucket

Wrangler

To get the list of lifecycle rules associated with your bucket, run the r2 bucket lifecycle list command.

npx wrangler r2 bucket lifecycle list <BUCKET_NAME>

S3 API

import S3 from "aws-sdk/clients/s3.js";

// Configure the S3 client to talk to R2.
const client = new S3({
	endpoint: "https://<account_id>.r2.cloudflarestorage.com",
	credentials: {
		accessKeyId: "<access_key_id>",
		secretAccessKey: "<access_key_secret>",
	},
	region: "auto",
});

// Get lifecycle configuration for bucket
console.log(
	await client
		.getBucketLifecycleConfiguration({
			Bucket: "bucketName",
		})
		.promise(),
);

Delete lifecycle rules from your bucket

Dashboard

  1. In the Cloudflare dashboard, go to the R2 object storage page.

    Go to Overview ↗
  2. Locate and select your bucket from the list.

  3. From the bucket page, select Settings.

  4. Under Object lifecycle rules, select the rules you would like to delete.

  5. When you are done, select Delete rule(s).

Wrangler

To remove a specific lifecycle rule from your bucket, run the r2 bucket lifecycle remove command.

npx wrangler r2 bucket lifecycle remove <BUCKET_NAME> --id <RULE_ID>

S3 API

import S3 from "aws-sdk/clients/s3.js";

// Configure the S3 client to talk to R2.
const client = new S3({
	endpoint: "https://<account_id>.r2.cloudflarestorage.com",
	credentials: {
		accessKeyId: "<access_key_id>",
		secretAccessKey: "<access_key_secret>",
	},
	region: "auto",
});

// Delete lifecycle configuration for bucket
await client
	.deleteBucketLifecycle({
		Bucket: "bucketName",
	})
	.promise();