Skip to content

Commit 99aa94a

Browse files
committed
Adding tabs
1 parent 06282b6 commit 99aa94a

File tree

1 file changed

+111
-34
lines changed

1 file changed

+111
-34
lines changed

articles/cosmos-db/how-to-time-to-live.md

Lines changed: 111 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ To create or enable TTL on a container see,
4444

4545
## Enable time to live on a container using SDK
4646

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)
4852

4953
```csharp
5054
// Create a new container with TTL enabled and without any expiration value
@@ -58,7 +62,9 @@ DocumentCollection ttlEnabledCollection = await client.CreateDocumentCollectionA
5862
collectionDefinition);
5963
```
6064

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)
6268

6369
```csharp
6470
// Create a new container with TTL enabled and without any expiration value
@@ -69,8 +75,13 @@ await client.GetDatabase("database").CreateContainerAsync(new ContainerPropertie
6975
DefaultTimeToLive = -1 //(never expire by default)
7076
});
7177
```
78+
---
79+
80+
### <a id="java-enable-noexpiry"></a> Java SDK
7281

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)
7485

7586
```java
7687
CosmosAsyncContainer container;
@@ -81,7 +92,9 @@ containerProperties.setDefaultTimeToLiveInSeconds(-1);
8192
container = database.createContainerIfNotExists(containerProperties, 400).block().getContainer();
8293
```
8394

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)
8598

8699
```java
87100
CosmosContainer container;
@@ -91,12 +104,17 @@ CosmosContainerProperties containerProperties = new CosmosContainerProperties("m
91104
containerProperties.defaultTimeToLive(-1);
92105
container = database.createContainerIfNotExists(containerProperties, 400).block().container();
93106
```
107+
---
94108

95109
## Set time to live on a container using SDK
96110

97111
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.
98112

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)
100118

101119
```csharp
102120
// Create a new container with TTL enabled and a 90 day expiration
@@ -110,7 +128,9 @@ DocumentCollection ttlEnabledCollection = await client.CreateDocumentCollectionA
110128
collectionDefinition;
111129
```
112130

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)
114134

115135
```csharp
116136
// Create a new container with TTL enabled and a 90 day expiration
@@ -121,22 +141,13 @@ await client.GetDatabase("database").CreateContainerAsync(new ContainerPropertie
121141
DefaultTimeToLive = 90 * 60 * 60 * 24; // expire all documents after 90 days
122142
});
123143
```
144+
---
124145

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
131147

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)
138149

139-
### <a id="java4-enable-defaultexpiry"></a>Java SDK V4 (Maven com.azure::azure-cosmos)
150+
Java SDK V4 (Maven com.azure::azure-cosmos)
140151

141152
```java
142153
CosmosAsyncContainer container;
@@ -147,7 +158,9 @@ containerProperties.setDefaultTimeToLiveInSeconds(90 * 60 * 60 * 24);
147158
container = database.createContainerIfNotExists(containerProperties, 400).block().getContainer();
148159
```
149160

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)
151164

152165
```java
153166
CosmosContainer container;
@@ -157,6 +170,21 @@ CosmosContainerProperties containerProperties = new CosmosContainerProperties("m
157170
containerProperties.defaultTimeToLive(90 * 60 * 60 * 24);
158171
container = database.createContainerIfNotExists(containerProperties, 400).block().container();
159172
```
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+
```
160188

161189
## Set time to live on an item
162190

@@ -235,7 +263,11 @@ const itemDefinition = {
235263
};
236264
```
237265

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)
239271

240272
```java
241273
// Include a property that serializes to "ttl" in JSON
@@ -270,7 +302,9 @@ SalesOrder salesOrder = new SalesOrder(
270302

271303
```
272304

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)
274308

275309
```java
276310
// Include a property that serializes to "ttl" in JSON
@@ -304,12 +338,17 @@ SalesOrder salesOrder = new SalesOrder(
304338
);
305339

306340
```
341+
---
307342

308343
## Reset time to live
309344

310345
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.
311346

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)
313352

314353
```csharp
315354
// This examples leverages the Sales Order class above.
@@ -323,7 +362,9 @@ readDocument.ttl = 60 * 30 * 30; // update time to live
323362
response = await client.ReplaceDocumentAsync(readDocument);
324363
```
325364

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)
327368

328369
```csharp
329370
// This examples leverages the Sales Order class above.
@@ -333,8 +374,13 @@ ItemResponse<SalesOrder> itemResponse = await client.GetContainer("database", "c
333374
itemResponse.Resource.ttl = 60 * 30 * 30; // update time to live
334375
await client.GetContainer("database", "container").ReplaceItemAsync(itemResponse.Resource, "SO05");
335376
```
377+
---
378+
379+
### <a id="java-enable-modifyitemexpiry"></a> Java SDK
336380

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)
338384

339385
```java
340386
// This examples leverages the Sales Order class above.
@@ -347,7 +393,9 @@ CosmosAsyncItemResponse<SalesOrder> itemResponse = container.readItem("SO05", ne
347393
}).block();
348394
```
349395

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)
351399

352400
```java
353401
// This examples leverages the Sales Order class above.
@@ -364,12 +412,17 @@ container.getItem("SO05", new PartitionKey("CO18009186470")).read()
364412
return container.createItem(salesOrder);
365413
}).block();
366414
```
415+
---
367416

368417
## Turn off time to live
369418

370419
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.
371420

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)
373426

374427
```csharp
375428
// This examples leverages the Sales Order class above.
@@ -384,7 +437,9 @@ readDocument.ttl = null; // inherit the default TTL of the container
384437
response = await client.ReplaceDocumentAsync(readDocument);
385438
```
386439

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)
388443

389444
```csharp
390445
// This examples leverages the Sales Order class above.
@@ -394,8 +449,13 @@ ItemResponse<SalesOrder> itemResponse = await client.GetContainer("database", "c
394449
itemResponse.Resource.ttl = null; // inherit the default TTL of the container
395450
await client.GetContainer("database", "container").ReplaceItemAsync(itemResponse.Resource, "SO05");
396451
```
452+
---
397453

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)
399459

400460
```java
401461
// This examples leverages the Sales Order class above.
@@ -408,7 +468,9 @@ CosmosAsyncItemResponse<SalesOrder> itemResponse = container.readItem("SO05", ne
408468
}).block();
409469
```
410470

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)
412474

413475
```java
414476
// This examples leverages the Sales Order class above.
@@ -425,12 +487,17 @@ container.getItem("SO05", new PartitionKey("CO18009186470")).read()
425487
return container.createItem(salesOrder);
426488
}).block();
427489
```
490+
---
428491

429492
## Disable time to live
430493

431494
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.
432495

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)
434501

435502
```csharp
436503
// Get the container, update DefaultTimeToLive to null
@@ -440,7 +507,9 @@ collection.DefaultTimeToLive = null;
440507
await client.ReplaceDocumentCollectionAsync(collection);
441508
```
442509

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)
444513

445514
```csharp
446515
// Get the container, update DefaultTimeToLive to null
@@ -449,8 +518,13 @@ ContainerResponse containerResponse = await client.GetContainer("database", "con
449518
containerResponse.Resource.DefaultTimeToLive = null;
450519
await client.GetContainer("database", "container").ReplaceContainerAsync(containerResponse.Resource);
451520
```
521+
---
452522

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)
454528

455529
```java
456530
CosmosContainerProperties containerProperties = new CosmosContainerProperties("myContainer", "/myPartitionKey");
@@ -460,7 +534,9 @@ containerProperties.setDefaultTimeToLiveInSeconds(null);
460534
container.replace(containerProperties).block();
461535
```
462536

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)
464540

465541
```java
466542
CosmosContainer container;
@@ -472,6 +548,7 @@ containerProperties.defaultTimeToLive(null);
472548
// Update container settings
473549
container = database.createContainerIfNotExists(containerProperties, 400).block().container();
474550
```
551+
---
475552

476553
## Next steps
477554

0 commit comments

Comments
 (0)