Skip to content

Commit 023da42

Browse files
committed
Document wrangler r2 bucket lifecycle command (list, add, remove, set)
1 parent 6e99f04 commit 023da42

File tree

2 files changed

+123
-9
lines changed

2 files changed

+123
-9
lines changed

src/content/docs/r2/buckets/object-lifecycles.mdx

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ pcx_content_type: how-to
55

66
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.
77

8+
A lifecycle configuration is a collection of lifecycle rules that define actions to apply to objects during their lifetime.
9+
810
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.
911

1012
## Behavior
1113

1214
- Objects will typically be removed from a bucket within 24 hours of the `x-amz-expiration` value.
13-
- 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.
15+
- 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.
1416
- An object is no longer billable once it has been deleted.
15-
- Buckets have a default lifecycle policy to expire multipart uploads seven days after initiation.
17+
- Buckets have a default lifecycle rule to expire multipart uploads seven days after initiation.
1618
- When an object is transitioned from Standard storage to Infrequent Access storage, a [Class A operation](/r2/pricing/#class-a-operations) is incurred.
1719

18-
## Configure your bucket's object lifecycle policy
20+
## Configure lifecycle rules for your bucket
1921

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

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

36+
### Wrangler
37+
38+
#### Set up Wrangler
39+
40+
To begin, install [`npm`](https://docs.npmjs.com/getting-started). Then [install Wrangler, the Developer Platform CLI](/workers/wrangler/install-and-update/).
41+
42+
#### Add a lifecycle rule
43+
44+
Log in to Wrangler with the [`wrangler login` command](/workers/wrangler/commands/#login). Then add a lifecycle rule to your bucket by running the [`r2 bucket lifecycle add` command](/workers/wrangler/commands/#lifecycle-add).
45+
46+
```sh
47+
npx wrangler r2 bucket lifecycle add <BUCKET_NAME> [OPTIONS]
48+
```
49+
50+
#### Set the lifecycle configuration
51+
52+
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).
53+
54+
```sh
55+
npx wrangler r2 bucket lifecycle set <BUCKET_NAME> --file <FILE_PATH>
56+
```
57+
58+
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).
59+
3460
### S3 API
3561

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

3864
```js title="Configure the S3 client to interact with R2"
3965
const client = new S3({
@@ -47,7 +73,7 @@ const client = new S3({
4773
});
4874
```
4975

50-
```javascript title="Configure the lifecycle policy for a bucket"
76+
```javascript title="Set the lifecycle configuration for a bucket"
5177
await client
5278
.putBucketLifecycleConfiguration({
5379
Bucket: "testBucket",
@@ -116,7 +142,17 @@ await client
116142
.promise();
117143
```
118144

119-
## Get a bucket's lifecycle policy
145+
## Get lifecycle rules for your bucket
146+
147+
### Wrangler
148+
149+
To get the list of lifecycle rules associated with your bucket, run the [`r2 bucket lifecycle list` command](/workers/wrangler/commands/#lifecycle-list).
150+
151+
```sh
152+
npx wrangler r2 bucket lifecycle list <BUCKET_NAME>
153+
```
154+
155+
### S3 API
120156

121157
```js
122158
import S3 from "aws-sdk/clients/s3.js";
@@ -132,7 +168,7 @@ const client = new S3({
132168
region: "auto",
133169
});
134170

135-
// Get lifecycle policy for bucket
171+
// Get lifecycle configuration for bucket
136172
console.log(
137173
await client
138174
.getBucketLifecycleConfiguration({
@@ -142,7 +178,7 @@ console.log(
142178
);
143179
```
144180

145-
## Delete a bucket's lifecycle policy
181+
## Delete lifecycle rules from your bucket
146182

147183
### Dashboard
148184

@@ -152,6 +188,14 @@ console.log(
152188
4. Under **Object lifecycle rules**, select the rules you would like to delete.
153189
5. When you are done, select **Delete rule(s)**.
154190

191+
### Wrangler
192+
193+
To remove a specific lifecycle rule from your bucket, run the [`r2 bucket lifecycle remove` command](/workers/wrangler/commands/#lifecycle-remove).
194+
195+
```sh
196+
npx wrangler r2 bucket lifecycle remove <BUCKET_NAME> --id <RULE_ID>
197+
```
198+
155199
### S3 API
156200

157201
```js
@@ -168,7 +212,7 @@ const client = new S3({
168212
region: "auto",
169213
});
170214

171-
// Delete lifecycle policy for bucket
215+
// Delete lifecycle configuration for bucket
172216
await client
173217
.deleteBucketLifecycle({
174218
Bucket: "bucketName",

src/content/docs/workers/wrangler/commands.mdx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,76 @@ wrangler r2 bucket notification list <NAME>
11031103
- `NAME` <Type text="string" /> <MetaInfo text="required" />
11041104
- The name of the R2 bucket to get event notification rules for.
11051105

1106+
### `lifecycle add`
1107+
1108+
Add an [object lifecycle](/r2/buckets/object-lifecycles/) rule to an R2 bucket.
1109+
1110+
```txt
1111+
wrangler r2 bucket lifecycle add <NAME> [OPTIONS]
1112+
```
1113+
1114+
- `NAME` <Type text="string" /> <MetaInfo text="required" />
1115+
- The name of the R2 bucket to add a lifecycle rule to.
1116+
- `--id` <Type text="string" /> <MetaInfo text="optional" />
1117+
- A unique identifier for the lifecycle rule.
1118+
- `--prefix` <Type text="string" /> <MetaInfo text="optional" />
1119+
- Prefix condition for the lifecycle rule (leave empty for all prefixes).
1120+
- `--expire-days` <Type text="number" /> <MetaInfo text="optional" />
1121+
- 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`.
1122+
- `--expire-date` <Type text="string" /> <MetaInfo text="optional" />
1123+
- 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`.
1124+
- `--ia-transition-days` <Type text="number" /> <MetaInfo text="optional" />
1125+
- 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`.
1126+
- `--ia-transition-date` <Type text="string" /> <MetaInfo text="optional" />
1127+
- 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`.
1128+
- `--abort-multipart-days` <Type text="number" /> <MetaInfo text="optional" />
1129+
- Sets the lifecycle rule action to abort incomplete multipart uploads after this number of days.
1130+
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
1131+
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
1132+
1133+
### `lifecycle remove`
1134+
1135+
Remove an [object lifecycle](/r2/buckets/object-lifecycles/) rule from an R2 bucket.
1136+
1137+
```txt
1138+
wrangler r2 bucket lifecycle remove <NAME> [OPTIONS]
1139+
```
1140+
1141+
- `NAME` <Type text="string" /> <MetaInfo text="required" />
1142+
- The name of the R2 bucket to remove a lifecycle rule from.
1143+
- `--id` <Type text="string" /> <MetaInfo text="required" />
1144+
- The unique identifier of the lifecycle rule to remove.
1145+
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
1146+
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
1147+
1148+
### `lifecycle list`
1149+
1150+
List [object lifecycle](/r2/buckets/object-lifecycles/) rules for an R2 bucket.
1151+
1152+
```txt
1153+
wrangler r2 bucket lifecycle list <NAME> [OPTIONS]
1154+
```
1155+
1156+
- `NAME` <Type text="string" /> <MetaInfo text="required" />
1157+
- The name of the R2 bucket to list lifecycle rules for.
1158+
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
1159+
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
1160+
1161+
### `lifecycle set`
1162+
1163+
Set the [object lifecycle](/r2/buckets/object-lifecycles/) configuration for an R2 bucket from a JSON file.
1164+
1165+
```txt
1166+
wrangler r2 bucket lifecycle set <NAME> [OPTIONS]
1167+
```
1168+
1169+
- `NAME` <Type text="string" /> <MetaInfo text="required" />
1170+
- The name of the R2 bucket to set lifecycle configuration for.
1171+
- `--file` <Type text="string" /> <MetaInfo text="required" />
1172+
- 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)).
1173+
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
1174+
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
1175+
11061176
### `sippy enable`
11071177

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

0 commit comments

Comments
 (0)