I want to delete the entire node by query like delete * WHERE user_id = "-KTruPWrYO9WFj-TF8Ft" How can I achieve this on firebase?
-KVpQFXnzQkzzrowHxGk
answer: "1"
question_number: 2
user_id: "-KTruPWrYO9WFj-TF8Ft"
-KVpQFXODhsAMJYFNjy7
answer: "4"
question_number: 25
user_id: "-KTruPWrYO9WFj-TF8Ft"
I want to delete the entire node by query like delete * WHERE user_id = "-KTruPWrYO9WFj-TF8Ft" How can I achieve this on firebase?
-KVpQFXnzQkzzrowHxGk
answer: "1"
question_number: 2
user_id: "-KTruPWrYO9WFj-TF8Ft"
-KVpQFXODhsAMJYFNjy7
answer: "4"
question_number: 25
user_id: "-KTruPWrYO9WFj-TF8Ft"
Share
Improve this question
edited Nov 6, 2016 at 6:15
Frank van Puffelen
600k85 gold badges890 silver badges860 bronze badges
asked Nov 5, 2016 at 18:15
Edyl Jay TempladoEdyl Jay Templado
1331 silver badge9 bronze badges
1 Answer
Reset to default 14To delete all references with child having some particular value first you will need to retrieve all keys ('-KVpQFXnzQkzzrowHxGk', '-KVpQFXnzQkzzrowHxGk' in your case) with equalTo
query and then delete those references with remove
function.
A sample code is here.
var ref = firebase.database(); //root reference to your data
ref.orderByChild('user_id').equalTo('-KTruPWrYO9WFj-TF8Ft')
.once('value').then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
//remove each child
ref.child(childSnapshot.key).remove();
});
});