As the title describes, I'm trying to change the TTL of a cosmos db table.
I couldn't find anything in c#/powershell/arm templates
Here is what I'm trying to achieve
The only thing I was able to find is the api call that is triggered in azure portal, but I'm wondering if it is safe to use this API directly?
Asked
Active
Viewed 1,721 times
2

Kira
- 1,153
- 4
- 28
- 63
-
You should be able to do that using any available Cosmos DB SDK. – Gaurav Mantri Jun 24 '20 at 08:47
2 Answers
2
In Cosmos DB Table API, Tables are essentially Containers thus you can use Cosmos DB SQL API SDK to manipulate the Table. Here's the sample code to do so:
var cosmosClient = new CosmosClient(CosmosConnectionString);
var database = cosmosClient.GetDatabase(Database);
var container = database.GetContainer("test");
var containerResponse = await container.ReadContainerAsync();
var containerProperties = containerResponse.Resource;
Console.WriteLine("Current TTL on the container is: " + containerProperties.DefaultTimeToLive);
containerProperties.DefaultTimeToLive = 120;//
containerResponse = await container.ReplaceContainerAsync(containerProperties);
containerProperties = containerResponse.Resource;
Console.WriteLine("Current TTL on the container is: " + containerProperties.DefaultTimeToLive);
Console.ReadKey();

Gaurav Mantri
- 128,066
- 12
- 206
- 241
-
I've already tested setting the TTL on a cosmos table in Azure portal and it actually works, and rows that are olter then the TTL I've put get automatically removed. So I'm not sure why you are saying it has no impact. – Kira Jun 30 '20 at 09:45
-
It will let you set the TTL on the table but from what I have been told (by Cosmos DB team) that entities will not automatically delete once the TTL has expired. Are you seeing the entities deleting from the tables automatically? – Gaurav Mantri Jun 30 '20 at 09:49
-
-
Oh! I am told otherwise. Great to know that. Let me reach out to the Cosmos DB team and seek clarification. – Gaurav Mantri Jul 02 '20 at 12:48
-
By the way I'm always getting Bad Request response for (ReadContainerAsync and ReplaceContainerAsync (tried to set a new value)) I think it's somehow blocked by MS – Kira Jul 02 '20 at 13:05
-
I am not sure about the error. BTW, I reached out to Cosmos DB team and they confirmed that TTL is indeed supported for entities in Cosmos Table, so I was wrong. I have updated my answer accordingly. – Gaurav Mantri Jul 03 '20 at 05:16
-
Getting `The connection string is missing a required property: AccountEndpoint` error while using the `new CosmosClient(CosmosConnectionString)` with `Assembly Microsoft.Azure.Cosmos.Client, Version=3.11.0.0` - How to fix it? – Sayantan Ghosh Jul 13 '20 at 09:20
-
1@SayantanGhosh...Please do not ask a question in comments. Instead ask a new question and provide all relevant details there. – Gaurav Mantri Jul 13 '20 at 09:23
-
Sure Gaurav. Added a question: https://stackoverflow.com/questions/62872969/the-connection-string-is-missing-a-required-property-accountendpoint-error-whil – Sayantan Ghosh Jul 13 '20 at 09:28
-
I see the per row ttl does not work for a table - can you please confirm if it is expected? https://stackoverflow.com/questions/62959117/cosmosdb-per-item-ttl-setting-throwing-exception-cannot-deserialize-the-current – Sayantan Ghosh Jul 18 '20 at 05:52
-
Seeting TTL is now possible using `Microsoft.Azure.Cosmos.Table` - pasted as answer below. – Sayantan Ghosh Aug 28 '20 at 05:04
0
Setting TTL is now supported through Microsoft.Azure.Cosmos.Table
directly with version >= 1.0.8.
// Get the table reference for table operations
CloudTable table = <tableClient>.GetTableReference(<tableName>);
table.CreateIfNotExists(defaultTimeToLive: <ttlInSeconds>);

Sayantan Ghosh
- 998
- 2
- 9
- 29