@@ -44,7 +44,11 @@ To create or enable TTL on a container see,
44
44
45
45
## Enable time to live on a container using SDK
46
46
47
- ### <a id =" dotnet-enable-noexpiry " ></a >.NET SDK V2 (Microsoft.Azure.DocumentDB)
47
+ ### <a id =" dotnet-enable-noexpiry " ></a > .NET SDK
48
+
49
+ # [ .NET SDK V2] ( #tab/dotnetv2 )
50
+
51
+ .NET SDK V2 (Microsoft.Azure.DocumentDB)
48
52
49
53
``` csharp
50
54
// Create a new container with TTL enabled and without any expiration value
@@ -58,7 +62,9 @@ DocumentCollection ttlEnabledCollection = await client.CreateDocumentCollectionA
58
62
collectionDefinition );
59
63
```
60
64
61
- ### <a id =" dotnet-enable-noexpiry " ></a >.NET SDK V3 (Microsoft.Azure.Cosmos)
65
+ # [ .NET SDK V3] ( #tab/dotnetv3 )
66
+
67
+ .NET SDK V3 (Microsoft.Azure.Cosmos)
62
68
63
69
``` csharp
64
70
// Create a new container with TTL enabled and without any expiration value
@@ -69,8 +75,13 @@ await client.GetDatabase("database").CreateContainerAsync(new ContainerPropertie
69
75
DefaultTimeToLive = - 1 // (never expire by default)
70
76
});
71
77
```
78
+ ---
79
+
80
+ ### <a id =" java-enable-noexpiry " ></a > Java SDK
72
81
73
- ### <a id =" java4-enable-noexpiry " ></a >Java SDK V4 (Maven com.azure::azure-cosmos)
82
+ # [ Java SDK V4] ( #tab/javav4 )
83
+
84
+ Java SDK V4 (Maven com.azure::azure-cosmos)
74
85
75
86
``` java
76
87
CosmosAsyncContainer container;
@@ -81,7 +92,9 @@ containerProperties.setDefaultTimeToLiveInSeconds(-1);
81
92
container = database. createContainerIfNotExists(containerProperties, 400 ). block(). getContainer();
82
93
```
83
94
84
- ### <a id =" java3-enable-noexpiry " ></a >Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
95
+ # [ Java SDK V3] ( #tab/javav3 )
96
+
97
+ Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
85
98
86
99
``` java
87
100
CosmosContainer container;
@@ -91,12 +104,17 @@ CosmosContainerProperties containerProperties = new CosmosContainerProperties("m
91
104
containerProperties. defaultTimeToLive(- 1 );
92
105
container = database. createContainerIfNotExists(containerProperties, 400 ). block(). container();
93
106
```
107
+ ---
94
108
95
109
## Set time to live on a container using SDK
96
110
97
111
To set the time to live on a container, you need to provide a non-zero positive number that indicates the time period in seconds. Based on the configured TTL value, all items in the container after the last modified timestamp of the item ` _ts ` are deleted.
98
112
99
- ### <a id =" dotnet-enable-withexpiry " ></a >.NET SDK V2 (Microsoft.Azure.DocumentDB)
113
+ ### <a id =" dotnet-enable-withexpiry " ></a > .NET SDK
114
+
115
+ # [ .NET SDK V2] ( #tab/dotnetv2 )
116
+
117
+ .NET SDK V2 (Microsoft.Azure.DocumentDB)
100
118
101
119
``` csharp
102
120
// Create a new container with TTL enabled and a 90 day expiration
@@ -110,7 +128,9 @@ DocumentCollection ttlEnabledCollection = await client.CreateDocumentCollectionA
110
128
collectionDefinition ;
111
129
```
112
130
113
- ### <a id="dotnet-enable-withexpiry"></a>.NET SDK V3 (Microsoft.Azure.Cosmos)
131
+ # [.NET SDK V3](#tab/dotnetv3)
132
+
133
+ .NET SDK V3 (Microsoft .Azure .Cosmos )
114
134
115
135
```csharp
116
136
// Create a new container with TTL enabled and a 90 day expiration
@@ -121,22 +141,13 @@ await client.GetDatabase("database").CreateContainerAsync(new ContainerPropertie
121
141
DefaultTimeToLive = 90 * 60 * 60 * 24 ; // expire all documents after 90 days
122
142
});
123
143
```
144
+ -- -
124
145
125
- ### <a id="nodejs-enable-withexpiry"></a>NodeJS SDK
126
-
127
- ```javascript
128
- const containerDefinition = {
129
- id : " sample container1" ,
130
- };
146
+ ### <a id="java-enable-defaultexpiry"></a> Java SDK
131
147
132
- async function createcontainerWithTTL (db : Database , containerDefinition : ContainerDefinition , collId : any , defaultTtl : number ) {
133
- containerDefinition .id = collId ;
134
- containerDefinition .defaultTtl = defaultTtl ;
135
- await db .containers .create (containerDefinition );
136
- }
137
- ```
148
+ # [Java SDK V4](#tab/javav4)
138
149
139
- ### <a id="java4-enable-defaultexpiry"></a> Java SDK V4 (Maven com.azure::azure-cosmos)
150
+ Java SDK V4 (Maven com .azure :: azure - cosmos )
140
151
141
152
```java
142
153
CosmosAsyncContainer container ;
@@ -147,7 +158,9 @@ containerProperties.setDefaultTimeToLiveInSeconds(90 * 60 * 60 * 24);
147
158
container = database .createContainerIfNotExists (containerProperties , 400 ).block ().getContainer ();
148
159
```
149
160
150
- ### <a id="java3-enable-defaultexpiry"></a>Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
161
+ # [Java SDK V3](#tab/javav3)
162
+
163
+ Java SDK V3 (Maven com .microsoft .azure :: azure - cosmos )
151
164
152
165
```java
153
166
CosmosContainer container ;
@@ -157,6 +170,21 @@ CosmosContainerProperties containerProperties = new CosmosContainerProperties("m
157
170
containerProperties .defaultTimeToLive (90 * 60 * 60 * 24 );
158
171
container = database .createContainerIfNotExists (containerProperties , 400 ).block ().container ();
159
172
```
173
+ -- -
174
+
175
+ ### <a id="nodejs-enable-withexpiry"></a>NodeJS SDK
176
+
177
+ ```javascript
178
+ const containerDefinition = {
179
+ id : " sample container1" ,
180
+ };
181
+
182
+ async function createcontainerWithTTL (db : Database , containerDefinition : ContainerDefinition , collId : any , defaultTtl : number ) {
183
+ containerDefinition .id = collId ;
184
+ containerDefinition .defaultTtl = defaultTtl ;
185
+ await db .containers .create (containerDefinition );
186
+ }
187
+ ```
160
188
161
189
## Set time to live on an item
162
190
@@ -235,7 +263,11 @@ const itemDefinition = {
235
263
};
236
264
```
237
265
238
- ### <a id="java4-enable-itemexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
266
+ ### <a id="java-set-ttl-item"></a> Java SDK
267
+
268
+ # [Java SDK V4](#tab/javav4)
269
+
270
+ Java SDK V4 (Maven com .azure :: azure - cosmos )
239
271
240
272
```java
241
273
// Include a property that serializes to "ttl" in JSON
@@ -270,7 +302,9 @@ SalesOrder salesOrder = new SalesOrder(
270
302
271
303
```
272
304
273
- ### <a id="java3-enable-itemexpiry"></a>Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
305
+ # [Java SDK V3](#tab/javav3)
306
+
307
+ Java SDK V3 (Maven com .microsoft .azure :: azure - cosmos )
274
308
275
309
```java
276
310
// Include a property that serializes to "ttl" in JSON
@@ -304,12 +338,17 @@ SalesOrder salesOrder = new SalesOrder(
304
338
);
305
339
306
340
```
341
+ -- -
307
342
308
343
## Reset time to live
309
344
310
345
You can reset the time to live on an item by performing a write or update operation on the item . The write or update operation will set the `_ts ` to the current time , and the TTL for the item to expire will begin again . If you wish to change the TTL of an item , you can update the field just as you update any other field .
311
346
312
- ### <a id="dotnet-extend-ttl-item"></a>.NET SDK V2 (Microsoft.Azure.DocumentDB)
347
+ ### <a id="dotnet-extend-ttl-item"></a> .NET SDK
348
+
349
+ # [.NET SDK V2](#tab/dotnetv2)
350
+
351
+ .NET SDK V2 (Microsoft .Azure .DocumentDB )
313
352
314
353
```csharp
315
354
// This examples leverages the Sales Order class above.
@@ -323,7 +362,9 @@ readDocument.ttl = 60 * 30 * 30; // update time to live
323
362
response = await client .ReplaceDocumentAsync (readDocument );
324
363
```
325
364
326
- ### <a id="dotnet-extend-ttl-item"></a>.NET SDK V3 (Microsoft.Azure.Cosmos)
365
+ # [.NET SDK V3](#tab/dotnetv3)
366
+
367
+ .NET SDK V3 (Microsoft .Azure .Cosmos )
327
368
328
369
```csharp
329
370
// This examples leverages the Sales Order class above.
@@ -333,8 +374,13 @@ ItemResponse<SalesOrder> itemResponse = await client.GetContainer("database", "c
333
374
itemResponse .Resource .ttl = 60 * 30 * 30 ; // update time to live
334
375
await client .GetContainer (" database" , " container" ).ReplaceItemAsync (itemResponse .Resource , " SO05" );
335
376
```
377
+ -- -
378
+
379
+ ### <a id="java-enable-modifyitemexpiry"></a> Java SDK
336
380
337
- ### <a id="java4-enable-modifyitemexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
381
+ # [Java SDK V4](#tab/javav4)
382
+
383
+ Java SDK V4 (Maven com .azure :: azure - cosmos )
338
384
339
385
```java
340
386
// This examples leverages the Sales Order class above.
@@ -347,7 +393,9 @@ CosmosAsyncItemResponse<SalesOrder> itemResponse = container.readItem("SO05", ne
347
393
}).block ();
348
394
```
349
395
350
- ### <a id="java3-enable-modifyitemexpiry"></a>Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
396
+ # [Java SDK V3](#tab/javav3)
397
+
398
+ SDK V3 (Maven com .microsoft .azure :: azure - cosmos )
351
399
352
400
```java
353
401
// This examples leverages the Sales Order class above.
@@ -364,12 +412,17 @@ container.getItem("SO05", new PartitionKey("CO18009186470")).read()
364
412
return container .createItem (salesOrder );
365
413
}).block ();
366
414
```
415
+ -- -
367
416
368
417
## Turn off time to live
369
418
370
419
If time to live has been set on an item and you no longer want that item to expire , then you can get the item , remove the TTL field , and replace the item on the server . When the TTL field is removed from the item , the default TTL value assigned to the container is applied to the item . Set the TTL value to -1 to prevent an item from expiring and to not inherit the TTL value from the container .
371
420
372
- ### <a id="dotnet-turn-off-ttl-item"></a>.NET SDK V2 (Microsoft.Azure.DocumentDB)
421
+ ### <a id="dotnet-turn-off-ttl-item"></a> .NET SDK
422
+
423
+ # [.NET SDK V2](#tab/dotnetv2)
424
+
425
+ .NET SDK V2 (Microsoft .Azure .DocumentDB )
373
426
374
427
```csharp
375
428
// This examples leverages the Sales Order class above.
@@ -384,7 +437,9 @@ readDocument.ttl = null; // inherit the default TTL of the container
384
437
response = await client .ReplaceDocumentAsync (readDocument );
385
438
```
386
439
387
- ### <a id =" dotnet-turn-off-ttl-item " ></a >.NET SDK V3 (Microsoft.Azure.Cosmos)
440
+ # [ .NET SDK V3] ( #tab/dotnetv3 )
441
+
442
+ .NET SDK V3 (Microsoft.Azure.Cosmos)
388
443
389
444
``` csharp
390
445
// This examples leverages the Sales Order class above.
@@ -394,8 +449,13 @@ ItemResponse<SalesOrder> itemResponse = await client.GetContainer("database", "c
394
449
itemResponse .Resource .ttl = null ; // inherit the default TTL of the container
395
450
await client .GetContainer (" database" , " container" ).ReplaceItemAsync (itemResponse .Resource , " SO05" );
396
451
```
452
+ ---
397
453
398
- ### <a id =" java4-enable-itemdefaultexpiry " ></a >Java SDK V4 (Maven com.azure::azure-cosmos)
454
+ ### <a id =" java-enable-itemdefaultexpiry " ></a > Java SDK
455
+
456
+ # [ Java SDK V4] ( #tab/javav4 )
457
+
458
+ Java SDK V4 (Maven com.azure::azure-cosmos)
399
459
400
460
``` java
401
461
// This examples leverages the Sales Order class above.
@@ -408,7 +468,9 @@ CosmosAsyncItemResponse<SalesOrder> itemResponse = container.readItem("SO05", ne
408
468
}). block();
409
469
```
410
470
411
- ### <a id =" java3-enable-itemdefaultexpiry " ></a >Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
471
+ # [ Java SDK V3] ( #tab/javav4 )
472
+
473
+ Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
412
474
413
475
``` java
414
476
// This examples leverages the Sales Order class above.
@@ -425,12 +487,17 @@ container.getItem("SO05", new PartitionKey("CO18009186470")).read()
425
487
return container. createItem(salesOrder);
426
488
}). block();
427
489
```
490
+ ---
428
491
429
492
## Disable time to live
430
493
431
494
To disable time to live on a container and stop the background process from checking for expired items, the ` DefaultTimeToLive ` property on the container should be deleted. Deleting this property is different from setting it to -1. When you set it to -1, new items added to the container will live forever, however you can override this value on specific items in the container. When you remove the TTL property from the container the items will never expire, even if there are they have explicitly overridden the previous default TTL value.
432
495
433
- ### <a id =" dotnet-disable-ttl " ></a >.NET SDK V2 (Microsoft.Azure.DocumentDB)
496
+ ### <a id =" dotnet-disable-ttl " ></a > .NET SDK
497
+
498
+ # [ .NET SDK V2] ( #tab/dotnetv2 )
499
+
500
+ .NET SDK V2 (Microsoft.Azure.DocumentDB)
434
501
435
502
``` csharp
436
503
// Get the container, update DefaultTimeToLive to null
@@ -440,7 +507,9 @@ collection.DefaultTimeToLive = null;
440
507
await client .ReplaceDocumentCollectionAsync (collection );
441
508
```
442
509
443
- ### <a id =" dotnet-disable-ttl " ></a >.NET SDK V3 (Microsoft.Azure.Cosmos)
510
+ # [ .NET SDK V3] ( #tab/dotnetv3 )
511
+
512
+ .NET SDK V3 (Microsoft.Azure.Cosmos)
444
513
445
514
``` csharp
446
515
// Get the container, update DefaultTimeToLive to null
@@ -449,8 +518,13 @@ ContainerResponse containerResponse = await client.GetContainer("database", "con
449
518
containerResponse .Resource .DefaultTimeToLive = null ;
450
519
await client .GetContainer (" database" , " container" ).ReplaceContainerAsync (containerResponse .Resource );
451
520
```
521
+ ---
452
522
453
- ### <a id =" java4-enable-disableexpiry " ></a >Java SDK V4 (Maven com.azure::azure-cosmos)
523
+ ### <a id =" java-enable-disableexpiry " ></a > Java SDK
524
+
525
+ # [ Java SDK V4] ( #tab/javav4 )
526
+
527
+ Java SDK V4 (Maven com.azure::azure-cosmos)
454
528
455
529
``` java
456
530
CosmosContainerProperties containerProperties = new CosmosContainerProperties (" myContainer" , " /myPartitionKey" );
@@ -460,7 +534,9 @@ containerProperties.setDefaultTimeToLiveInSeconds(null);
460
534
container. replace(containerProperties). block();
461
535
```
462
536
463
- ### <a id =" java3-enable-disableexpiry " ></a >Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
537
+ # [ Java SDK V3] ( #tab/javav3 )
538
+
539
+ Java SDK V3 (Maven com.microsoft.azure::azure-cosmos)
464
540
465
541
``` java
466
542
CosmosContainer container;
@@ -472,6 +548,7 @@ containerProperties.defaultTimeToLive(null);
472
548
// Update container settings
473
549
container = database. createContainerIfNotExists(containerProperties, 400 ). block(). container();
474
550
```
551
+ ---
475
552
476
553
## Next steps
477
554
0 commit comments