Is there any TTL option on documents for Firebase Firestore . Where documents get auto deleted after that amount time
4 Answers
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:
Adding a
expirationTimestamp
property to your documents.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.
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).

- 565,676
- 79
- 828
- 807
-
2Also, you can now use Cloud Tasks to schedule a function that performs the future deletion of a document. – Doug Stevenson Jun 01 '19 at 20:16
-
Cloud Tasks is need payment to use? – luke cross Jun 12 '20 at 19:29
-
1https://cloud.google.com/tasks#pricing "First 1 Million operations per month are free" – Frank van Puffelen Jun 12 '20 at 20:16
-
Sounds like a great new question @Trax – Frank van Puffelen Aug 04 '22 at 19:26
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:
- Go to the Cloud Firestore Time-to-live page in the Google Cloud Platform Console.
- Go to the Time-to-live page.
- Click Create Policy.
- Enter a collection group name and the timestamp field name (
expireAt
in our example). - Click Create.

- 11,015
- 6
- 55
- 79
-
2
-
"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
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.

- 71
- 4