Himanshu
Himanshu

Reputation: 462

How to delete a node from firebase by referring it's child node?

"Subscription" : {
    "1519197182611" : {
      "address" : "Mumbai",
      "dateTime" : "Feb 21, 2018 12:42:57 PM",
      "name" : "ABC",
      "phone" : "1264897809",
      "uIdpId" : "123456"
    },
    "1519197186551" : {
      "address" : "Mumbai",
      "dateTime" : "Feb 21, 2018 12:42:57 PM",
      "name" : "DCF",
      "phone" : "1264897809",
      "uIdpId" : "7897"
    },
    "1519197360198" : {
      "address" : "Mumbai",
      "dateTime" : "Feb 21, 2018 12:45:54 PM",
      "name" : "XYZ",
      "phone" : "1264897809",
      "uIdpId" : "45656"
    }
  }

I want to delete the node through whose name is ABC. So how can i proceed further. I got stuck here.

Upvotes: 1

Views: 410

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Try this:

DatabaseReference data = FirebaseDatabase.getInstance().getReference().child("Subscription");
data.orderByChild("name").equalTo(ABC).addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
      for(DataSnapshot data: dataSnapshot.getChildren()){
            data.getRef().removeValue();

                  }

            }

        @Override
       public void onCancelled(DatabaseError databaseError) {

               }
          });

The snapshot is at child Subscription, then you use orderByChild("name").equalTo(valuehere) which is the condition that name should be equal to ABC for example.

Then using the for loop you iterate inside the random pushids, getRef() will give you the reference of this source location and removeValue() will remove the node.

Upvotes: 3

Related Questions