Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions src/content/docs/r2/buckets/object-lifecycles.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ pcx_content_type: how-to

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

- Objects will typically be removed from a bucket within 24 hours of the `x-amz-expiration` value.
- When a lifecycle policy is applied that deletes objects, newly uploaded objects' `x-amz-expiration` value immediately reflects the expiration based on the new rules, but existing objects may experience a delay. Most objects will be transitioned within 24 hours but may take longer depending on the number of objects in the bucket. While objects are being migrated, you may see old applied rules from the previous policy.
- When a lifecycle configuration is applied that deletes objects, newly uploaded objects' `x-amz-expiration` value immediately reflects the expiration based on the new rules, but existing objects may experience a delay. Most objects will be transitioned within 24 hours but may take longer depending on the number of objects in the bucket. While objects are being migrated, you may see old applied rules from the previous configuration.
- An object is no longer billable once it has been deleted.
- Buckets have a default lifecycle policy to expire multipart uploads seven days after initiation.
- Buckets have a default lifecycle rule to expire multipart uploads seven days after initiation.
- When an object is transitioned from Standard storage to Infrequent Access storage, a [Class A operation](/r2/pricing/#class-a-operations) is incurred.

## Configure your bucket's object lifecycle policy
## 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.

Expand All @@ -31,9 +33,28 @@ When you create an object lifecycle rule, you can specify which prefix you would
5. Fill out the fields for the new rule.
6. When you are done, select **Add rule**.

### Wrangler

1. Install [`npm`](https://docs.npmjs.com/getting-started).
2. Install [Wrangler, the Developer Platform CLI](/workers/wrangler/install-and-update/).
3. Log in to Wrangler with the [`wrangler login` command](/workers/wrangler/commands/#login).
4. Add a lifecycle rule to your bucket by running the [`r2 bucket lifecycle add` command](/workers/wrangler/commands/#lifecycle-add).

```sh
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](/workers/wrangler/commands/#lifecycle-set).

```sh
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](/api/operations/r2-put-bucket-lifecycle-configuration).

### S3 API

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

```js title="Configure the S3 client to interact with R2"
const client = new S3({
Expand All @@ -47,7 +68,7 @@ const client = new S3({
});
```

```javascript title="Configure the lifecycle policy for a bucket"
```javascript title="Set the lifecycle configuration for a bucket"
await client
.putBucketLifecycleConfiguration({
Bucket: "testBucket",
Expand Down Expand Up @@ -116,7 +137,17 @@ await client
.promise();
```

## Get a bucket's lifecycle policy
## 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](/workers/wrangler/commands/#lifecycle-list).

```sh
npx wrangler r2 bucket lifecycle list <BUCKET_NAME>
```

### S3 API

```js
import S3 from "aws-sdk/clients/s3.js";
Expand All @@ -132,7 +163,7 @@ const client = new S3({
region: "auto",
});

// Get lifecycle policy for bucket
// Get lifecycle configuration for bucket
console.log(
await client
.getBucketLifecycleConfiguration({
Expand All @@ -142,7 +173,7 @@ console.log(
);
```

## Delete a bucket's lifecycle policy
## Delete lifecycle rules from your bucket

### Dashboard

Expand All @@ -152,6 +183,14 @@ console.log(
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](/workers/wrangler/commands/#lifecycle-remove).

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

### S3 API

```js
Expand All @@ -168,7 +207,7 @@ const client = new S3({
region: "auto",
});

// Delete lifecycle policy for bucket
// Delete lifecycle configuration for bucket
await client
.deleteBucketLifecycle({
Bucket: "bucketName",
Expand Down
72 changes: 72 additions & 0 deletions src/content/docs/workers/wrangler/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,78 @@ wrangler r2 bucket notification list <NAME>
- `NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of the R2 bucket to get event notification rules for.

### `lifecycle add`

Add an [object lifecycle](/r2/buckets/object-lifecycles/) rule to an R2 bucket.

```txt
wrangler r2 bucket lifecycle add <NAME> [OPTIONS]
```

- `NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of the R2 bucket to add a lifecycle rule to.
- `--id` <Type text="string" /> <MetaInfo text="optional" />
- A unique identifier for the lifecycle rule.
- `--prefix` <Type text="string" /> <MetaInfo text="optional" />
- Prefix condition for the lifecycle rule (leave empty for all prefixes).
- `--expire-days` <Type text="number" /> <MetaInfo text="optional" />
- Sets the lifecycle rule action to expire objects after this number of days. Note you can provide only one of `--expire-days` or `--expire-date`.
- `--expire-date` <Type text="string" /> <MetaInfo text="optional" />
- Sets the lifecycle rule action to expire objects after this date (YYYY-MM-DD). Note you can provide only one of `--expire-days` or `--expire-date`.
- `--ia-transition-days` <Type text="number" /> <MetaInfo text="optional" />
- Sets the lifecycle rule action to transition objects to Infrequent Access storage after this number of days. Note you can provide only one of `--ia-transition-days` or `--ia-transition-date`.
- `--ia-transition-date` <Type text="string" /> <MetaInfo text="optional" />
- Sets the lifecycle rule action to transition objects to Infrequent Access storage after this date (YYYY-MM-DD). Note you can provide only one of `--ia-transition-days` or `--ia-transition-date`.
- `--abort-multipart-days` <Type text="number" /> <MetaInfo text="optional" />
- Sets the lifecycle rule action to abort incomplete multipart uploads after this number of days.
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
- `--force` <Type text="boolean" /> <MetaInfo text="optional" />
- Skip confirmation when adding the lifecycle rule.

### `lifecycle remove`

Remove an [object lifecycle](/r2/buckets/object-lifecycles/) rule from an R2 bucket.

```txt
wrangler r2 bucket lifecycle remove <NAME> [OPTIONS]
```

- `NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of the R2 bucket to remove a lifecycle rule from.
- `--id` <Type text="string" /> <MetaInfo text="required" />
- The unique identifier of the lifecycle rule to remove.
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).

### `lifecycle list`

List [object lifecycle](/r2/buckets/object-lifecycles/) rules for an R2 bucket.

```txt
wrangler r2 bucket lifecycle list <NAME> [OPTIONS]
```

- `NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of the R2 bucket to list lifecycle rules for.
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).

### `lifecycle set`

Set the [object lifecycle](/r2/buckets/object-lifecycles/) configuration for an R2 bucket from a JSON file.

```txt
wrangler r2 bucket lifecycle set <NAME> [OPTIONS]
```

- `NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of the R2 bucket to set lifecycle configuration for.
- `--file` <Type text="string" /> <MetaInfo text="required" />
- Path to the JSON file containing lifecycle configuration (file must be in format of request body of [put object lifecycle configuration API](/api/operations/r2-put-bucket-lifecycle-configuration)).
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).

### `sippy enable`

Enable [Sippy](/r2/data-migration/sippy/) incremental migration for a bucket.
Expand Down
Loading