Home » Android » android – How to delete multiple nodes in one request in firebase?

android – How to delete multiple nodes in one request in firebase?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have two nodes from one root and I want to delete the data from both of them in one request. Both sub-nodes has the same key. I tried this:

Firebase firebaseRef = new Firebase(<root_path>);

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(<path_to_first_node>, key);
childUpdates.put(<path_to_second_node>, key);

listToRemoveRef.updateChildren(childUpdates, null);

But it removed data from only the first node

How to&Answers:

It looks like you’re using the updateChildren function wrong. What you want to do is this

Firebase firebaseRef = new Firebase(<root_path>);

Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("<path_to_first_node>" + key, null);
childUpdates.put("<path_to_second_node>" + key, null);

listToRemoveRef.updateChildren(childUpdates);

The second parameter to updateChildren doesn’t set the value to null it is an optional completion listener (see documentation). So instead of passing null to it on the last line, you can just omit it.