0

I wanted to delete, data older than 5 minutes from the Fire-base Database table automatically. Is there an option to configure that in the fire-base.

All these days I kind of trigger it from the app, but this wont be accurate as there are not much people using my app and the time interval between 2 users using it may go beyond the 5 minutes interval so the data I have wont be consistent.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Amalan Dhananjayan
  • 2,277
  • 1
  • 34
  • 47
  • Also check [this question](https://stackoverflow.com/questions/32004582/delete-firebase-data-older-than-2-hours) – André Kool Jun 18 '18 at 07:13

2 Answers2

1

Unfortunately, Firebase does not have a way of scheduling tasks. Unless you have your own server or hosting that allows cron jobs, you will have to use the App Engine or Compute Engine. You can schedule cron jobs in either one to perform the task for you. The Firebase blog actually has a great post on how to do this: How to Schedule (Cron) Jobs with Cloud Functions for Firebase

The basic gist of it is, set up a scheduled task on a server that has permissions to access your Firebase database. Have the task perform the cleanup code that you use in your app.

If you do use the App Engine or Compute Engine, be careful about instance charges. They give a free tier that is sufficient for small jobs, however. I'd check it out.

Timothy Winters
  • 5,481
  • 1
  • 15
  • 18
0

First you can add a column which store time during data insert each time. Then you compare each data time with current time. If current time is greater then 5 min than call delete function through child Key which can delete that specific data from table. I want to delete top first row from table in firebase Database

$searchdata = "Data";
    $fetchdata = $database->getReference($searchdata)->getValue();
    $found = 0;
    $array=0;
        if($fetchdata > 0)
        {
            foreach($fetchdata as $Key => $row)
            {
                $t=time() - $row["Time"];
                if($t <= 250)
                {
                    $del = "Data/".$Key;
                    $DelRef = $database->getReference($del)->remove();
                    $found++;
                }
            }
        }