Skip to main content

Firestore TTL - Time to Live

Introduction

As we all know that Firestore charges you for storing data in the database. Prior to Firestore TTL, if we want to remove unused data, we have to write a cron job to remove the data, which charges us both reading and deleting fees. To some high-traffic collection that rarely supports the application logic like webhookLogs, apiLogs, etc, we need to remove the data after a certain period of time. Firestore TTL is a feature that allows you to set a time to live for documents in a collection.

You can see the official documentation here.

How to set up Firestore TTL

The official documentation is quite clear in explaining when you will need this, its limitations, and how to set it up. However, they missed the most important part, how to configure with firestore.indexes.json:


{
"indexes": [
// Your indexes as usual
],
"fieldOverrides": [
{
"collectionGroup": "webhookLogs",
"fieldPath": "expiredAt",
"ttl": true,
"indexes": [{"order": "ASCENDING", "queryScope": "COLLECTION_GROUP"}]
}
]
}

In the above example, we set up a TTL for the webhookLogs collection.

  • The expiredAt field is the field that Firestore will use to determine when to delete the document. It will take place with 24 hours after the expiredAt field value. Remember that you need to manually set this field value when you create the document, or updating it. Feel free to choose your policy.
  • The ttl field is set to true to enable the TTL feature. The indexes field is an array of indexes that Firestore will use to query the collection.

FAQs

Will it remove data match and existed before the TTL policy created?

Yes, it will remove within 24 hours.

Will this cost you?

Yes, this will cost you the delete fee according to Firebase pricing, but in the long run, you will save. It will cost you hundreds of dollars, even thousands.