Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an expiration date for Firestore documents [duplicate]

How can I add a future date and time to a firestore document and add a function when that time expires? For example today I create a document and record that time and then I want to register that fifteen days later the publicado field will be false. It's possible?

I am creating a document in firestore in the following way:

component.ts

import { Component, OnInit } from '@angular/core';
import { FirebaseService } from '../../../services/firebase.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

forma: FormGroup;

constructor( fb: FormBuilder, private fs: FirebaseService ) {
  this.forma = fb.group ({
    fechaCreacion: [ firebase.firestore.FieldValue.serverTimestamp() ],
    publicado: [ true ],
  })
}

agregarAviso() {
  this.fs.addAviso(this.forma.value);
}

firebase.service.ts

constructor( private afs: AngularFirestore ) {}

addAviso(aviso){
  this.afs.collection('avisos').add(aviso);
}

As a result I get the following: enter image description here

like image 966
Paco Zevallos Avatar asked Sep 01 '25 04:09

Paco Zevallos


1 Answers

Firestore doesn't have a concept of a TTL (time to live) for documents. Documents exist forever until some code directly deletes them.

You could write some code to periodically query for documents that should be expired, and you will have to accept that they may finally be deleted after the timestamp, by up to the amount of time your process runs periodically. You will likely want to run this on a backend you control, with a scheduler that you control, using the Firebase Admin SDK.

like image 166
Doug Stevenson Avatar answered Sep 02 '25 18:09

Doug Stevenson