8

I'm trying to make a firebase cloud function to delete a node from Firebase Database. The log messages show that the function executed "ok" but it doesn't seem to remove any element from the database. I wrote the function taking help from the accepted answer in How to delete data in Firebase? Here is the snippet of the code

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

//path is defined as the value to be deleted,
console.log("Deleting element " + path);
var ref = admin.database().ref("/")
ref.orderByValue().equalTo(path).on('child_added', function(snapshot) {
    console.log("Snapshot.ref = " + snapshot.ref);
    snapshot.ref.remove();
    return;
});

Also, in the above code, "Deleting element path_value" does show up in the log but Snapshot.ref = ... doesn't show up.

I don't have enough credits to embed images yet so here is a link to my database Structure of Firebase Database

2
  • I think the selection is wrong. Commented Jul 20, 2017 at 21:02
  • How are you triggering the function? Cloud Functions for Firebase run once they are triggered by an event. What is the event you're using to trigger this? An HTTP trigger? A Database write event? Commented Jul 20, 2017 at 21:51

1 Answer 1

15

I think the selection is wrong. Double check that ref.orderByValue().equalTo(path) is actually equal to something.

ref.once('value')
  .then(function(dataSnapshot) {
    // handle read data.
  });

https://firebase.google.com/docs/reference/admin/node/admin.database.Reference

var adaRef = admin.database().ref('users/ada');
adaRef.remove()
  .then(function() {
    console.log("Remove succeeded.")
  })
  .catch(function(error) {
    console.log("Remove failed: " + error.message)
  });
Sign up to request clarification or add additional context in comments.

3 Comments

This works, although not quite as expected; it deleted all the contents of my database. The log shows "Snapshot.ref = application.firebaseio.com". Could you help me navigate to the correct reference location?
In Firebase, you can navigate to a node, then copy the URL. This should help you find the exact location you want. In your example, var ref = admin.database().ref("/"), "/" should be a path to the nodes you want to delete.
Thanks. That should help me find my way through. I have accepted your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.