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! Go to 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.
You can create a TTL policy through the Google Cloud Console or the Google Cloud CLI. Here's a brief overview:
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:
expireAt
or any other name that you choose.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.
You can create a TTL policy through the Google Cloud Console or the Google Cloud CLI. Here's a brief overview:
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