2

I'm trying to delete all nodes with a date greater than '2017-04-05' with a bulk operation with a firebase function. Can you spot what I'm doing wrong here?

The 2 nodes that should get deleted are the ones in red: enter image description here

Here's the code that is failing - can you see what's wrong? Also, I'm concerned about the performance of this (I'll only run it once in a while though). If there are millions of games in the list, should that concern me if I only run this once a day?

exports.remove = functions.https.onRequest((req, res) => {

    const deleteBeforeDate = req.query.deleteBeforeDate;

    var ref = admin.database().ref('games');

    var keysToDelete = {};
    for (var game in this.games) {

        var date = items[i]['.date'];
        if(date.value > '2017-04-05'){
            keysToDelete[game.key] = null;
        }
    }
    this.ref.update(keysToDelete);
});

Thank you very much,

Mike

2
  • 1
    this.games is not populated anywhere. Commented Apr 16, 2017 at 4:24
  • 1
    Also, there is no child .date (the period seems a typo). Commented Apr 16, 2017 at 4:26

1 Answer 1

9

To determine the keys to delete, you'll need to attach a listener. Since that is needed, you might as well create a query that selects the correct children and deletes only those:

var ref = admin.database().ref('games');
var deleteAfterDate = ref.orderByChild('date').startAt('2017-04-05');

deleteAfterDate.once('value').then(function(snapshot) {
  var updates = {};
  snapshot.forEach(function(child) {
    updates[child.key] = null;
  });
  ref.update(updates);
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you Frank! So nice to have expert help on this! I tried a first run with it and I got a timeout error 'finished with status timeout' in the firebase function log. I notice that the ref.orderByChild('date') seems to skip the nodes in between them (the actual nodes I want to delete). If I do orderByChild on something that is really a grandchild, does that still work? I'm running the code again and it's now not returning from the call for the last 10 minutes - and I only have 16 games in the database in all...
Your JSON shows the dates as direct children on the nodes you're trying to delete. On a reasonable number of nodes (with the structure you've shown, that anywhere up to a few hundreds of thousands) this should not take 10 minutes. But it depends on many factors. Can you reproduce the problem in something more easily debuggable, such as a jsbin?
Correct, the json shows that the nodes I want to delete are the direct parents of date, but we've selected the grandparent node of date in the ref variable, and calling orderByChild on date is two levels lower. I'm new to this though, so I'm probably not understanding. I will look into jsbin (never heard of it) and try to make debugging easier! :)
Ah... that's what you mean by grandchild. Queries run on the children of the location where you execute them, so date is a direct property under that. You can also query lower level children, but that's not needed here.
Oh of course! Otherwise you'd never have more than one item in your results I guess. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.