Configure and manage Time to Live in Azure Cosmos DB (2024)

  • Article

APPLIES TO: Configure and manage Time to Live in Azure Cosmos DB (1)NoSQL

In Azure Cosmos DB, you can choose to configure Time to Live (TTL) at the container level, or you can override it at an item level after setting for the container. You can configure TTL for a container by using Azure portal or the language-specific SDKs. Item level TTL overrides can be configured by using the SDKs.

This article's content is related to Azure Cosmos DB transactional store TTL. If you are looking for analitycal store TTL, that enables NoETL HTAP scenarios through Azure Synapse Link, please click here.

Enable time to live on a container using the Azure portal

Use the following steps to enable time to live on a container with no expiration. Enabling TTL at the container level to allow the same value to be overridden at an individual item's level. You can also set the TTL by entering a non-zero value for seconds.

  1. Sign in to the Azure portal.

  2. Create a new Azure Cosmos DB account or select an existing account.

  3. Open the Data Explorer pane.

  4. Select an existing container, expand the Settings tab and modify the following values:

    • Under Setting find, Time to Live.

    • Based on your requirement, you can:

      • Turn off this setting
      • Set it to On (no default) or
      • Turn On with a TTL value specified in seconds.
    • Select Save to save the changes.

    Configure and manage Time to Live in Azure Cosmos DB (2)

  • When DefaultTimeToLive is null, then your Time to Live is Off
  • When DefaultTimeToLive is -1 then, your Time to Live setting is On (No default)
  • When DefaultTimeToLive has any other Int value (except 0), then your Time to Live setting is On. The server will automatically delete items based on the configured value.

Enable time to live on a container using Azure CLI or Azure PowerShell

To create or enable TTL on a container see,

  • Create a container with TTL using Azure CLI
  • Create a container with TTL using PowerShell

Enable time to live on a container using an SDK

  • .NET SDK v3
  • Java SDK v4
  • Node SDK
  • Python SDK
Database database = client.GetDatabase("database");ContainerProperties properties = new (){ Id = "container", PartitionKeyPath = "/customerId", // Never expire by default DefaultTimeToLive = -1};// Create a new container with TTL enabled and without any expiration valueContainer container = await database .CreateContainerAsync(properties);

Set time to live on a container using an SDK

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.

  • .NET SDK v3
  • Java SDK v4
  • Node SDK
  • Python SDK
Database database = client.GetDatabase("database");ContainerProperties properties = new (){ Id = "container", PartitionKeyPath = "/customerId", // Expire all documents after 90 days DefaultTimeToLive = 90 * 60 * 60 * 24};// Create a new container with TTL enabled and without any expiration valueContainer container = await database .CreateContainerAsync(properties);

Set time to live on an item using the Portal

In addition to setting a default time to live on a container, you can set a time to live for an item. Setting time to live at the item level will override the default TTL of the item in that container.

  • To set the TTL on an item, you need to provide a non-zero positive number, which indicates the period, in seconds, to expire the item after the last modified timestamp of the item _ts. You can provide a -1 as well when the item shouldn't expire.

  • If the item doesn't have a TTL field, then by default, the TTL set to the container will apply to the item.

  • If TTL is disabled at the container level, the TTL field on the item will be ignored until TTL is re-enabled on the container.

Use the following steps to enable time to live on an item:

  1. Sign in to the Azure portal.

  2. Create a new Azure Cosmos DB account or select an existing account.

  3. Open the Data Explorer pane.

  4. Select an existing container, expand it and modify the following values:

    • Open the Scale & Settings window.
    • Under Setting find, Time to Live.
    • Select On (no default) or select On and set a TTL value.
    • Select Save to save the changes.
  5. Next navigate to the item for which you want to set time to live, add the ttl property and select Update.

    { "id": "1", "_rid": "Jic9ANWdO-EFAAAAAAAAAA==", "_self": "dbs/Jic9AA==/colls/Jic9ANWdO-E=/docs/Jic9ANWdO-EFAAAAAAAAAA==/", "_etag": "\"0d00b23f-0000-0000-0000-5c7712e80000\"", "_attachments": "attachments/", "ttl": 10, "_ts": 1551307496}

Set time to live on an item using an SDK

  • .NET SDK v3
  • Java SDK v4
  • Node SDK
  • Python SDK
public record SalesOrder(string id, string customerId, int ttl);
Container container = database.GetContainer("container");SalesOrder item = new ( "SO05", "CO18009186470" // Expire sales order in 30 days using "ttl" property ttl: 60 * 60 * 24 * 30);await container.CreateItemAsync<SalesOrder>(item);

Reset time to live using an SDK

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.

  • .NET SDK v3
  • Java SDK v4
  • Node SDK
  • Python SDK
SalesOrder item = await container.ReadItemAsync<SalesOrder>( "SO05", new PartitionKey("CO18009186470"));// Update ttl to 2 hoursSalesOrder modifiedItem = item with { ttl = 60 * 60 * 2 };await container.ReplaceItemAsync<SalesOrder>( modifiedItem, "SO05", new PartitionKey("CO18009186470") );

Disable time to live using an SDK

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 they have explicitly overridden the previous default TTL value.

  • .NET SDK v3
  • Java SDK v4
  • Node SDK
  • Python SDK
ContainerProperties properties = await container.ReadContainerAsync();// Disable ttl at container-levelproperties.DefaultTimeToLive = null;await container.ReplaceContainerAsync(properties);

Next steps

Learn more about time to live in the following article:

  • Time to live
Configure and manage Time to Live in Azure Cosmos DB (2024)
Top Articles
Ua 6133
Garen 2V2V2V2 Build
Custom Screensaver On The Non-touch Kindle 4
Swimgs Yuzzle Wuzzle Yups Wits Sadie Plant Tune 3 Tabs Winnie The Pooh Halloween Bob The Builder Christmas Autumns Cow Dog Pig Tim Cook’s Birthday Buff Work It Out Wombats Pineview Playtime Chronicles Day Of The Dead The Alpha Baa Baa Twinkle
Promotional Code For Spades Royale
Beacon Schnider
Collision Masters Fairbanks
Tyrunt
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Shaniki Hernandez Cam
CSC error CS0006: Metadata file 'SonarAnalyzer.dll' could not be found
83600 Block Of 11Th Street East Palmdale Ca
Weather In Moon Township 10 Days
Bbc 5Live Schedule
Garrick Joker'' Hastings Sentenced
Tamilblasters 2023
Pwc Transparency Report
Little Rock Arkansas Craigslist
Nier Automata Chapter Select Unlock
Erskine Plus Portal
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis - NFL
Louisiana Sportsman Classifieds Guns
Skyward Login Jennings County
Sound Of Freedom Showtimes Near Cinelux Almaden Cafe & Lounge
Water Days For Modesto Ca
Army Oubs
Labby Memorial Funeral Homes Leesville Obituaries
Keurig Refillable Pods Walmart
Cbssports Rankings
zom 100 mangadex - WebNovel
Rimworld Prison Break
Dragger Games For The Brain
European city that's best to visit from the UK by train has amazing beer
Craigslist Wilkes Barre Pa Pets
John Deere 44 Snowblower Parts Manual
Gr86 Forums
Vip Lounge Odu
Chris Provost Daughter Addie
Clark County Ky Busted Newspaper
Empires And Puzzles Dark Chest
Xxn Abbreviation List 2023
Pokemon Reborn Gyms
Weekly Math Review Q2 7 Answer Key
COVID-19/Coronavirus Assistance Programs | FindHelp.org
Costco Gas Foster City
Online-Reservierungen - Booqable Vermietungssoftware
Streameast Io Soccer
Lorton Transfer Station
Cult Collectibles - True Crime, Cults, and Murderabilia
Gonzalo Lira Net Worth
Union Supply Direct Wisconsin
Ajpw Sugar Glider Worth
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6456

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.