Search documentationEsc

Time-to-live

Set TTL at the container or item level using TimeToLiveItem and FullItem.

Cosmos DB lets you expire items automatically using a time-to-live (TTL) value. With Cosmos Repository you can set the TTL at the container level (default for every item) or at the item level (per-row override).

Container-level default

Use WithContainerDefaultTimeToLive on the container builder:

options.ContainerBuilder.Configure<BankAccount>(
x => x.WithContainerDefaultTimeToLive(TimeSpan.FromHours(2)));

Every item in the BankAccount container expires after two hours unless overridden.

Item-level override

Inherit from TimeToLiveItem (or FullItem) and set TimeToLive per item:

BankAccount currentBankAccount = await repository.CreateAsync(
new BankAccount
{
Name = "Current Account",
Balance = 500.0,
TimeToLive = TimeSpan.FromHours(4)
});

This row will live for four hours rather than the container default of two.

Disabling TTL for a single item

Set TimeToLive = TimeSpan.FromSeconds(-1) to opt the item out of expiration entirely:

BankAccount currentBankAccount = await repository.CreateAsync(
new BankAccount
{
Name = "Current Account",
Balance = 500.0,
TimeToLive = TimeSpan.FromSeconds(-1)
});

Sample BankAccount class

The full example referenced above (used in the OptimisticConcurrencyControl sample):

using Microsoft.Azure.CosmosRepository;
using Microsoft.Azure.CosmosRepository.Attributes;
namespace OptimisticConcurrencyControl;
[Container("accounts")]
[PartitionKeyPath("/id")]
public class BankAccount : FullItem
{
public string Name { get; set; } = string.Empty;
public double Balance { get; set; }
public void Withdraw(double amount)
{
if (Balance - amount < 0.0)
throw new InvalidOperationException("Cannot go overdrawn");
Balance -= amount;
}
public void Deposit(double amount) => Balance += amount;
public override string ToString() =>
$"Account (Name = {Name}, Balance = {Balance}, Etag = {Etag})";
}