Skip to content

Commit 2b50508

Browse files
authored
Document wrangler r2 bucket lifecycle command (list, add, remove, set) (#18074)
* Document wrangler r2 bucket lifecycle command (list, add, remove, set) * Address PR copy changes, add force option to wrangler lifecycle add docs
1 parent aa92ee4 commit 2b50508

File tree

2 files changed

+120
-9
lines changed

2 files changed

+120
-9
lines changed

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

Lines changed: 48 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,28 @@ 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+
1. Install [`npm`](https://docs.npmjs.com/getting-started).
39+
2. Install [Wrangler, the Developer Platform CLI](/workers/wrangler/install-and-update/).
40+
3. Log in to Wrangler with the [`wrangler login` command](/workers/wrangler/commands/#login).
41+
4. Add a lifecycle rule to your bucket by running the [`r2 bucket lifecycle add` command](/workers/wrangler/commands/#lifecycle-add).
42+
43+
```sh
44+
npx wrangler r2 bucket lifecycle add <BUCKET_NAME> [OPTIONS]
45+
```
46+
47+
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).
48+
49+
```sh
50+
npx wrangler r2 bucket lifecycle set <BUCKET_NAME> --file <FILE_PATH>
51+
```
52+
53+
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).
54+
3455
### S3 API
3556

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

3859
```js title="Configure the S3 client to interact with R2"
3960
const client = new S3({
@@ -47,7 +68,7 @@ const client = new S3({
4768
});
4869
```
4970

50-
```javascript title="Configure the lifecycle policy for a bucket"
71+
```javascript title="Set the lifecycle configuration for a bucket"
5172
await client
5273
.putBucketLifecycleConfiguration({
5374
Bucket: "testBucket",
@@ -116,7 +137,17 @@ await client
116137
.promise();
117138
```
118139

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

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

135-
// Get lifecycle policy for bucket
166+
// Get lifecycle configuration for bucket
136167
console.log(
137168
await client
138169
.getBucketLifecycleConfiguration({
@@ -142,7 +173,7 @@ console.log(
142173
);
143174
```
144175

145-
## Delete a bucket's lifecycle policy
176+
## Delete lifecycle rules from your bucket
146177

147178
### Dashboard
148179

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

186+
### Wrangler
187+
188+
To remove a specific lifecycle rule from your bucket, run the [`r2 bucket lifecycle remove` command](/workers/wrangler/commands/#lifecycle-remove).
189+
190+
```sh
191+
npx wrangler r2 bucket lifecycle remove <BUCKET_NAME> --id <RULE_ID>
192+
```
193+
155194
### S3 API
156195

157196
```js
@@ -168,7 +207,7 @@ const client = new S3({
168207
region: "auto",
169208
});
170209

171-
// Delete lifecycle policy for bucket
210+
// Delete lifecycle configuration for bucket
172211
await client
173212
.deleteBucketLifecycle({
174213
Bucket: "bucketName",

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,78 @@ 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+
- `--force` <Type text="boolean" /> <MetaInfo text="optional" />
1133+
- Skip confirmation when adding the lifecycle rule.
1134+
1135+
### `lifecycle remove`
1136+
1137+
Remove an [object lifecycle](/r2/buckets/object-lifecycles/) rule from an R2 bucket.
1138+
1139+
```txt
1140+
wrangler r2 bucket lifecycle remove <NAME> [OPTIONS]
1141+
```
1142+
1143+
- `NAME` <Type text="string" /> <MetaInfo text="required" />
1144+
- The name of the R2 bucket to remove a lifecycle rule from.
1145+
- `--id` <Type text="string" /> <MetaInfo text="required" />
1146+
- The unique identifier of the lifecycle rule to remove.
1147+
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
1148+
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
1149+
1150+
### `lifecycle list`
1151+
1152+
List [object lifecycle](/r2/buckets/object-lifecycles/) rules for an R2 bucket.
1153+
1154+
```txt
1155+
wrangler r2 bucket lifecycle list <NAME> [OPTIONS]
1156+
```
1157+
1158+
- `NAME` <Type text="string" /> <MetaInfo text="required" />
1159+
- The name of the R2 bucket to list lifecycle rules for.
1160+
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
1161+
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
1162+
1163+
### `lifecycle set`
1164+
1165+
Set the [object lifecycle](/r2/buckets/object-lifecycles/) configuration for an R2 bucket from a JSON file.
1166+
1167+
```txt
1168+
wrangler r2 bucket lifecycle set <NAME> [OPTIONS]
1169+
```
1170+
1171+
- `NAME` <Type text="string" /> <MetaInfo text="required" />
1172+
- The name of the R2 bucket to set lifecycle configuration for.
1173+
- `--file` <Type text="string" /> <MetaInfo text="required" />
1174+
- 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)).
1175+
- `--jurisdiction` <Type text="string" /> <MetaInfo text="optional" />
1176+
- The jurisdiction where the bucket exists, if a jurisdiction has been specified. Refer to [jurisdictional restrictions](/r2/reference/data-location/#jurisdictional-restrictions).
1177+
11061178
### `sippy enable`
11071179

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

0 commit comments

Comments
 (0)