0

Description:

I have created a meeting app using ReactJS. In the database, 1 Document contains the data of a single meeting. I want to delete all documents which has been created 2 days. This process can either run all the time in database, or I would prefer, that the process will run every day at 12:00am, which is at night...

   const getlink=()=>{

    var currentDate = new Date(new Date().getTime() + 48 * 60 * 60 * 1000);
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    const exp = day + "/" + month + "/" + year

    const uid=uuid()
    db.collection("rooms").doc(uid).set({
        roomId:uid,
        expire:exp
    })
    alert("Your (RoomId) is: \n"+uid+"\n\n⚠️ Your Room will expire on - "+exp)

    history.push("/")

}

Above is the code I have used for creating a room and saving to a database...

Note: I am using Functional Components... but not Class Based components.. So please try to provide a Functional based answer

Could anyone please help me figure out how to delete the rooms which has been created before 2 days..

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • The first link refers to REALTIME DATABASE whereas I want FIRESTORE DATABASE.. The second link is not opening in my browser. Could you please provide a summary of the content over there, If it is about Firestore.. – Spandan Manna Aug 19 '21 at 16:50

1 Answers1

0

You should query all the documents older than 2 days and then iterate the query results and remove them individually.

I would suggest adding a field in your document that stores the timestamp of the creation of that document so you can then query those documents older than 2 days.

To run this process in a specific time you might try using Cloud Functions, you can use Firebase scheduled function or alternatively you can set up a cron job that executes the program every x minutes.

In this other post there are some answers that might be helpful, please check this answer as an example to do the above.

Sergi
  • 135
  • 7