Jump to Content
Databases

Manage storage costs by automatically deleting expired data using Firestore Time-To-Live (TTL)

October 17, 2022
Hansi Mou

Software Engineer, Firestore

Josué Urbina

Software Engineer, Firestore

We are thrilled to announce that we have added time-to-live (TTL) support for both Firestore Native and Datastore mode!

Use Firestore TTL policies to remove out-of-date data from the database. You can think of it as a managed deletion mechanism built-into Firestore. Once documents or entities are considered expired, they will be eligible for deletion. Similar to direct DELETE operations, it will also notify all external services (ex:  Function Triggers, etc.), upon a deletion event.

Common use cases

  • Garbage collection. TTL can be handy if you have data that has a well-defined lifecycle for a document.

  • Support time relevant features natively. You can rely on TTLs if you want to build features relying on ephemeral data.

  • Security and privacy compliance. There are some regulations that require data retention for no longer than a certain time. You will have the flexibility to configure different expiration at the document level, which can help you meet the requirements from varying sources.

Example walkthrough

Sounds like a good candidate for your application? Let’s walk through an example to see how it works from end to end. The example below uses documents and collections, but it works similarly for entities and kinds.

Assume you have a database that saves lots of documents in collection Chats and some of them will be useless at some point in the future.

https://storage.googleapis.com/gweb-cloudblog-publish/images/1_ttl.max-1900x1900.jpg

First of all, you need to decide on a field to use as the TTL field, and that field must contain a  timestamp value. For example, you can choose to designate the expireAt field as a TTL field, even  if your documents don’t contain values for this field yet. 

There are two ways of configuring TTL policies:

  • Use the gcloud CLI. You can find some sample commands to view and modify TTL policies. 

  • Use the Google Cloud Console. You can navigate to the Firestore Time-to-live configuration page to configure a new policy.

https://storage.googleapis.com/gweb-cloudblog-publish/images/2_ttl.max-800x800.jpg

Now that you have configured TTL policies, the documents should be updated with the TTL field if not already. In this case it is expireAt that serves as TTL field.

https://storage.googleapis.com/gweb-cloudblog-publish/images/3_ttl.max-1900x1900.jpg

That’s everything you need to do. Once a document expires, it’s eligible for deletion, and Firestore will perform the deletion on your behalf.

Want to learn more? Check out the documentation and happy databasing.


Special thanks to Minh Nguyen, Lead Product Manager for Firestore, and Joseph Batchik, Software Engineer for Firestore, for contributing to this post.

Posted in