Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete/remove nodes on Firebase

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.

like image 539
jkamdjou Avatar asked Oct 23 '14 21:10

jkamdjou


People also ask

How do I delete a node from Firebase realtime database?

The simplest way to delete data is to call remove() on a reference to the location of that data. You can also delete by specifying null as the value for another write operation such as set() or update() .

How do I delete a collection in Firebase?

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors.

How do I delete multiple data in Firebase?

In order to delete multiple entries from your database, you need to know all those locations (refernces). So with other words, in the way you add data, you should also delete it. This method atomically deletes all those entries.


1 Answers

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(); 
like image 85
Frank van Puffelen Avatar answered Sep 29 '22 19:09

Frank van Puffelen