1

I would like to delete any data that is older that two hours. I know this question is already answered here Delete firebase data older than 2 hours

Answer is:

var ref = new Firebase('https://yours.firebaseio.com/path/to/items/');
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var old = ref.orderByChild('timestamp').endAt(cutoff).limitToLast(1);
var listener = old.on('child_added', function(snapshot) {
    snapshot.ref().remove();
});

But can someone help in converting it to nodejs function which i can deploy to firebase cloud function.

Community
  • 1
  • 1
Rishabh Chandel
  • 725
  • 1
  • 11
  • 28
  • 1
    Blog post the specifically addresses this: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html. See also this [Cloud Functions sample](https://github.com/firebase/functions-samples/tree/master/delete-unused-accounts-cron) – Bob Snyder Apr 21 '17 at 20:28
  • 1
    See http://stackoverflow.com/questions/42790735/cloud-functions-for-firebase-trigger-on-time – Frank van Puffelen Apr 22 '17 at 02:40

1 Answers1

2

Cloud Function is like a trigger in database, it needs event to stimulate the function to do the works.

The info for all the events can be looked at here.

For your case, I suggest using http event

After that, you can make a cron job from your own server, or GAE, or anything else that call a http request to trigger your function.

Faruk
  • 5,438
  • 3
  • 30
  • 46
  • 1
    This is exactly right. You'll want to use a cron job. I've had success using an Cloud Function with Zapier. Zapier lets you fire off HTTP requests or push data to Firebase on a cron. – Chris Esplin Apr 21 '17 at 22:50