How to delete/remove nodes on Firebase

JavascriptWebFirebase

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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();

Solution 2 - Javascript

I hope this code will help someone - it is from https://firebase.google.com/docs/reference/js/firebase.database.Reference"> official Google Firebase documentation:

var adaRef = firebase.database().ref('users/ada');
adaRef.remove()
  .then(function() {
    console.log("Remove succeeded.")
  })
  .catch(function(error) {
    console.log("Remove failed: " + error.message)
  });

Solution 3 - Javascript

Firebase.remove() like probably most Firebase methods is asynchronous, thus you have to listen to events to know when something happened:

parent = ref.parent()
parent.on('child_removed', function (snapshot) {
    // removed!
})
ref.remove()

According to Firebase docs it should work even if you lose network connection. If you want to know when the change has been actually synchronized with Firebase servers, you can pass a callback function to Firebase.remove method:

ref.remove(function (error) {
    if (!error) {
        // removed!
    }
}

Solution 4 - Javascript

To remove a record.

 var db = firebase.database();                   
 var ref = db.ref(); 
 var survey=db.ref(path+'/'+path);    //Eg path is company/employee                
 survey.child(key).remove();          //Eg key is employee id

Solution 5 - Javascript

As others have noted the call to .remove() is asynchronous. We should all be aware nothing happens 'instantly', even if it is at the speed of light.

What you mean by 'instantly' is that the next line of code should be able to execute after the call to .remove(). With asynchronous operations the next line may be when the data has been removed, it may not - it is totally down to chance and the amount of time that has elapsed.

.remove() takes one parameter a callback function to help deal with this situation to perform operations after we know that the operation has been completed (with or without an error). .push() takes two params, a value and a callback just like .remove().

Here is your example code with modifications:

ref = new Firebase("myfirebase.com")

ref.push({key:val}, function(error){
  //do stuff after push completed
});

// deletes all data pushed so far
ref.remove(function(error){
  //do stuff after removal
});

Solution 6 - Javascript

In case you are using axios and trying via a service call.

URL: https://react-16-demo.firebaseio.com/
Schema Name: todos
Key: -Lhu8a0uoSRixdmECYPE

axios.delete(`https://react-16-demo.firebaseio.com/todos/-Lhu8a0uoSRixdmECYPE.json`). then();

can help.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjkamdjouView Question on Stackoverflow
Solution 1 - JavascriptFrank van PuffelenView Answer on Stackoverflow
Solution 2 - JavascriptUlaView Answer on Stackoverflow
Solution 3 - JavascriptszimekView Answer on Stackoverflow
Solution 4 - JavascriptSpalbarView Answer on Stackoverflow
Solution 5 - JavascriptDavidView Answer on Stackoverflow
Solution 6 - JavascriptVed PrakashView Answer on Stackoverflow