23

Is there any TTL option on documents for Firebase Firestore . Where documents get auto deleted after that amount time

Mahi Tej Gvp
  • 984
  • 1
  • 14
  • 34

4 Answers4

26

Update (2022-07-26): Firestore just added the option to set a time-to-live policy on collection groups. I'm still leaving the custom approach below, as those give you control over the expunge moment which (for now) isn't possible with the built-in feature.


The easiest way to build it yourself is by:

  1. Adding a expirationTimestamp property to your documents.

  2. Denying read of documents whose expiration has passed in your security rules.

     match /collection/{document} {
       allow read: if resource.data.expirationTimestamp > request.time.date();
     }
    

    Unfortunately this means that you won't be able to query the collection anymore. You'll need to access the individual documents.

  3. Periodically run Cloud Functions code to delete expired documents.

Also see Doug's excellent blog post describing this process: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
5

Firestore TTL policies is now available in preview

https://cloud.google.com/firestore/docs/ttl

Dror
  • 51
  • 1
  • 3
3

As of July 26th 2022, TTL Policies for Firestore were released as a preview feature (which means its not ready for production). Update Oct 2022: The TTL Policies feature has graduated from preview to General Availability, which means it should now be ready for production!

In order to use TTL Policies in Firestore, make sure that your documents have a field (of type Date & Time) to define the expiration date of that document (let's name the field expireAt for example).

And then follow the steps outlined in the documentation:

  1. Go to the Cloud Firestore Time-to-live page in the Google Cloud Platform Console.
  2. Go to the Time-to-live page.
  3. Click Create Policy.
  4. Enter a collection group name and the timestamp field name (expireAt in our example).
  5. Click Create.
  • 2
    does it mean its not safe to be used for Production? – showtime Jul 29 '22 at 18:40
  • "Deletion through TTL is not an instantaneous process. Expired documents continue to appear in queries and lookup requests until the TTL process actually deletes them. TTL trades deletion timeliness for the benefit of reduced total cost of ownership for deletions. Data is typically deleted within 72 hours after its expiration date." It doesn't seem all that good, does it? This is as per official docs, at the time of writing,https://firebase.google.com/docs/firestore/ttl – Anshuman Kumar Oct 21 '22 at 11:15
  • @dontdownvoteme the product has now graduated to GA. I've updated my answer – Rosário Pereira Fernandes Oct 21 '22 at 18:41
  • Of type Date & Time but in which format?, number, string, ISO?, Does it make a difference? – Lagistos Feb 08 '23 at 14:25
  • @Lagistos If you take a look at the [data types page of Firestore](https://firebase.google.com/docs/firestore/manage-data/data-types) you'll see "Date & Time" listed as one of the supported types.The Firebase console and SDKs might call it "timestamp". – Rosário Pereira Fernandes Feb 08 '23 at 15:41
1

If you are still using the firebase cli to deploy the firestore config (rules, indexes etc.) then here is how you set the ttl in the firebase.index.json file on a specific field in a collection:

{
    "indexes": [
    // your indexes. 
    ],
    "fieldOverrides":
    [
        {
            "collectionGroup": "name-of-collection",
            "fieldPath": "name-field-to-add-ttl",
            "ttl": true,
            "indexes": [
                {
                    "order": "ASCENDING",
                    "fieldPath": "COLLECTION"
                },
                {
                    "order": "DESCENDING",
                    "fieldPath": "COLLECTION"
                },
                {
                    "fieldPath": "COLLECTION", "arrayConfig": "CONTAINS"
                }
            ]
        }
    ]
}

You also need to allow TTL it the GCP console (not firestore console) because you are charged extra for it.