Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

TTL policy for Firestore in Datamode with Terraform

Am I right that in order to create TTL policy for collections/entities in Firestore at the moment the only way is through Google UI or gcloud CLI?

Can you confirm it?

Thanks

Solved Solved
0 6 2,607
1 ACCEPTED SOLUTION

@Salvo, Thanks for pointing this out. Sorry for the confusion. My previous reply did not account for provide current capabilities of Firestore regarding TTL. Firestore does support Time-to-Live (TTL) policies natively, a feature that allows you to designate a specific field as the expiration time for documents in a given collection group.

Creating a TTL Policy

You can create a TTL policy through the Google Cloud Console or the Google Cloud CLI. Here's a brief overview:

  1. Designate a Field: Select a document field as the expiration time for documents in a collection group. This field must be of type "Date and time."
  2. Create Policy: Navigate to the Firestore Time-to-Live page in the Google Cloud console, click "Create Policy," enter the collection group name and timestamp field name, and click "Create."
  3. Monitor Deletions: Utilize Cloud Monitoring to view metrics about TTL-driven deletions.

Constraints and Considerations

  • Only one field per collection group can be marked as a TTL field.
  • TTL deletion is not instantaneous; expired documents may continue to appear in queries until actually deleted.
  • Deleting a document through TTL does not delete subcollections under that document.
  • Deletion through TTL triggers Cloud Functions Firestore triggers.

View solution in original post

6 REPLIES 6

Firestore does not have a built-in TTL feature, but you can implement a TTL-like behavior by using a combination of a timestamp field in your documents and a scheduled Cloud Function.

Here are the steps involved:

  1. Add a field to your documents that represents the expiration time. This field can be called expireAt or any other name that you choose.
  2. Create a scheduled Cloud Function that runs at regular intervals (e.g., every hour).
  3. In the Cloud Function, query for documents that have expired based on the timestamp field and delete them.

Here is a code example of how you might implement a TTL-like behavior using Firestore and Cloud Functions:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();

// Scheduled function that runs every hour
exports.deleteExpiredDocuments = functions.pubsub.schedule('every 1 hours').onRun(async (context) => {
const now = admin.firestore.Timestamp.now();
const query = firestore.collection('users').where('expireAt', '<', now);

const querySnapshot = await query.get();
const batch = firestore.batch();

querySnapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});

await batch.commit();
console.log('Deleted expired documents');
});

In this example, the expireAt field in the documents would represent the expiration time, and the scheduled function would delete documents that have expired.

Please note that the implementation of TTL-like behavior in Firestore would require careful consideration of your specific use case and may have implications for your application's behavior and costs. For example, if you are using a lot of documents, you may want to consider using a different approach, such as using a third-party service to manage TTL for your documents.

This document seems to say differently https://cloud.google.com/firestore/docs/ttl

@Salvo, Thanks for pointing this out. Sorry for the confusion. My previous reply did not account for provide current capabilities of Firestore regarding TTL. Firestore does support Time-to-Live (TTL) policies natively, a feature that allows you to designate a specific field as the expiration time for documents in a given collection group.

Creating a TTL Policy

You can create a TTL policy through the Google Cloud Console or the Google Cloud CLI. Here's a brief overview:

  1. Designate a Field: Select a document field as the expiration time for documents in a collection group. This field must be of type "Date and time."
  2. Create Policy: Navigate to the Firestore Time-to-Live page in the Google Cloud console, click "Create Policy," enter the collection group name and timestamp field name, and click "Create."
  3. Monitor Deletions: Utilize Cloud Monitoring to view metrics about TTL-driven deletions.

Constraints and Considerations

  • Only one field per collection group can be marked as a TTL field.
  • TTL deletion is not instantaneous; expired documents may continue to appear in queries until actually deleted.
  • Deleting a document through TTL does not delete subcollections under that document.
  • Deletion through TTL triggers Cloud Functions Firestore triggers.

Ok Thank you.

I have another question. I have created a firestore database. It is visible via the gcloud CLI. I can describe it but it is not visible from the UI. 

Did you name your database something other than (default)? At the moment, only the (default) database is visible in the console. Non-default databases are a preview feature and console support is not yet available.

It makes sense. Yes i have named it differently than default. Thanks a lot