How does Cosmos TTL works, I want to purge the items which are older than 365 days

Nikhil Kuttan 5 Reputation points

I want to purge the items which are older than 365 days, If I set 31536000 sec
will it flush all the items or it will flush only those items which were not modified in last 365 days

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,531 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vahid Ghafarpour 20,480 Reputation points

    In Azure Cosmos DB, the Time-to-Live (TTL) feature works based on the item's last modification time, not the creation time. The TTL value specifies the duration in seconds after the last update to an item, after which the item is eligible for deletion.

    When an item is created or updated in Cosmos DB, the TTL value is reset. If the TTL value is set to a specific duration, such as 365 days (31536000 seconds), and no further updates are made to the item within that duration, the item will be marked for deletion.

    1 person found this answer helpful.

  2. Oury Ba-MSFT 17,791 Reputation points Microsoft Employee

    @Nikhil Kuttan Thank you for reaching out.

    What if a new item comes in will it remain in the container.
    basically, I want to only keep last 365 days data.

    In addition to the above answer.

    If a new item is added to the container, it will be subject to the same TTL policy as the existing items. If the new item is not modified within the last 365 days, it will be automatically removed from the container when the TTL expires. If the new item is modified within the last 365 days, it will remain in the container until the TTL expires.

    To only keep the last 365 days of data in the container, you can set the TTL value to 31536000 seconds (which is equivalent to 365 days). This will automatically remove all items that were last modified more than 365 days ago. Any new items that are added to the container will also be subject to the same TTL policy and will be automatically removed when the TTL expires.

    Time to Live (TTL) in Azure Cosmos DB