How to delete/remove nodes on Firebase

jkamdjou picture jkamdjou · Oct 23, 2014 · Viewed 145.7k times · Source

I'm using Firebase for a web app. It's written in plain Javascript using no external libraries.

I can "push" and retrieve data with '.on("child_added")', but '.remove()' does not work the way it says it should. According to the API,

"Firebase.remove() - Remove the data at this Firebase location. Any data at child locations will also be deleted. The effect of the delete will be visible immediately."

However, the remove is not occurring immediately; only when the entire script is done running. I need to remove and then use the cleared tree immediately after.

Example code:

ref = new Firebase("myfirebase.com") //works
ref.push({key:val}) //works

ref.on('child_added', function(snapshot){
//do stuff
}); //works

ref.remove()
//does not remove until the entire script/page is done

There is a similar post here but I am not using Ember libraries, and even so it seems like a workaround for what should be as simple as the API explains it to be.

Answer

Frank van Puffelen picture Frank van Puffelen · Oct 24, 2014

The problem is that you call remove on the root of your Firebase:

ref = new Firebase("myfirebase.com")
ref.remove();

This will remove the entire Firebase through the API.

You'll typically want to remove specific child nodes under it though, which you do with:

ref.child(key).remove();