Skip to main content

How to Set a Time to Live on an Azure Cosmos DB Container

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Azure Cosmos DB can delete old items for you. Give a container a time to live, in seconds, and every item disappears that many seconds after its last write, with no cleanup job of your own. Here's how to set it in Data Explorer and with the Azure CLI, and what I saw happen to existing and new items.

Microsoft's documentation describes time to live (TTL) like this: Azure Cosmos DB automatically removes these items after the time period, since the time they were last modified. Each item's last write time is its _ts property, so every write starts the clock again. Data that only matters for a while, such as sessions, carts or logs, is what it's for.

My example is a small notes app's container, notes in the database notesdb. To watch TTL work without waiting days, I set it to 60 seconds; then I set it to an hour with the Azure CLI.

Step 1: Open the Container's Settings​

In your account's Data Explorer, expand the database and the container, and select Settings under the container. The Settings tab opens with Time to Live at the top.

Step 2: Choose the Time to Live​

Time to Live has three options:

OptionWhat happensIn code, the container's DefaultTimeToLive
OffItems never expire, and a ttl on an item is ignorednull
On (no default)Items don't expire, unless an item has its own ttl-1
On, with a number of secondsEvery item expires that many seconds after its last write; an item's own ttl overrides itThe seconds

Select On, type 60 and select Save in the toolbar:

The notes container's Settings tab in Data Explorer, with the tabs Home, notes.Items and notes.Settings, Time to Live set to On, outlined in red, 60 second(s) in the box below, outlined in red, the explanation The system will automatically delete items based on the TTL value (in seconds) you provide, and Save in the toolbar outlined in red

The setting applies to the container from that moment, including to the items already in it.

Step 3: Watch the Items Expire​

The container held one note, note-1, last written about 50 seconds before I saved the setting. When I listed the items 23 seconds after saving, it was gone:

The notes container's Items list with only the /id column header and no items

By then its last write was about 70 seconds old, more than the 60-second time to live, so it had expired. Microsoft's documentation: An item no longer appears in query responses immediately after the TTL expires, even if it's not yet permanently deleted from the container. The deletion itself runs in the background.

Then I wrote two new notes with the console app from How to Connect to Azure Cosmos DB with Microsoft Entra ID in C#:

Output
Saved note-1: 201 Created, 6.1 RU
Saved note-2: 201 Created, 6.1 RU
Read note-1: "Buy milk", 1 RU

Both were new, 201 Created, because the expired note-1 no longer existed. In Items, the new note-1 has the _ts 1795506879, the number of seconds since 1 January 1970: 07:54:39 UTC.

The notes container's Items list with note-1 and note-2, and note-1 open on the right with id note-1, text Buy milk, the system properties _rid, _self, _etag and _attachments, and _ts 1795506879

I refreshed the list at 07:56:13, a minute and a half later. Both notes were gone:

The notes container's Items list empty again, with only the /id column header

Do It with the Azure CLI​

The container's time to live is the defaultTtl of its resource. Read it:

Terminal
az cosmosdb sql container show \
--account-name cosmos-howto-4224 \
--resource-group rg-maf-servicedesk \
--database-name notesdb \
--name notes \
--query "resource.defaultTtl"
Output
60

The 60 seconds from the portal. To change it, az cosmosdb sql container update takes --ttl, in seconds. An hour is 3600:

Terminal
az cosmosdb sql container update \
--account-name cosmos-howto-4224 \
--resource-group rg-maf-servicedesk \
--database-name notesdb \
--name notes \
--ttl 3600 \
--query "{container:name, defaultTtl:resource.defaultTtl}"
Output
{
"container": "notes",
"defaultTtl": 3600
}

Use your own account, resource group, database and container. The CLI reference describes --ttl: If the value is missing or set to "-1", items don't expire. If the value is set to "n", items will expire "n" seconds after last modified time. Both commands manage the container with your Azure role, so they work with key-based authentication turned off. az cosmosdb sql container create takes the same --ttl, to set it when you create a container.

Give One Item Its Own Time to Live​

An item can override the container's setting with a ttl property of its own, in seconds. Microsoft's rules:

  • Item-level TTL only comes into play if the TTL property is set at the container level and is NOT set to null, so the container must be On or On (no default).
  • An item with "ttl": -1 never expires, even if the container has a time to live.
  • With the container On (no default), only items that have a ttl expire.

Common Mistakes​

MistakeWhat happensDo this instead
Turning TTL on for a container that already holds dataEvery item older than the TTL disappears at onceCheck the oldest _ts first, or start with a long TTL
Setting ttl on items while the container's TTL is OffThe items never expireSet the container to On or On (no default)
Treating the TTL as an exact deadlineExpired items disappear from reads at once, but are deleted in the backgroundDon't rely on the exact moment the storage is freed
Measuring from when the item was createdEvery write starts the time againExpect an item to live TTL seconds after its last write

FAQ​

Does deleting expired items cost anything?​

On a serverless account, yes: the deletion of expired items is charged in RUs at the same rate as delete item operations. On a provisioned-throughput account, it uses throughput that your requests didn't need.

What's the longest time to live?​

The maximum value for TTL is 2,147,483,647 seconds, the approximate equivalent of 24,855 days or 68 years.

Can I bring an expired item back?​

Not from the container: it no longer appears in reads or queries. Write it again if you still have it.

Conclusion​

A time to live makes Azure Cosmos DB delete items a set number of seconds after their last write. In Data Explorer, set Time to Live to On with the seconds on the container's Settings tab and select Save; with the Azure CLI, use az cosmosdb sql container update --ttl. It applies to existing items at once, so turn it on with care, and use an item's own ttl for items that need a different lifetime.

Additional Resources​