Remove a specific value from Firebase Database

10,989

First, get the specific value that you want to delete, then remove it.

If you do not know about the key of that object, you have to query to get the object key from DataSnapshot. This can be done by using getKey() method (ex. postsnapshot.getKey())

Or, check out my Firebase Helper Classes or Firebase Helper Class Tutorial

mdatabaseReference.child("users").orderByKey().equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {

  for (DataSnapshot postsnapshot :dataSnapshot.getChildren()) {

    String key = postsnapshot.getKey();
    dataSnapshot.getRef().removeValue();

 }
Share:
10,989
Kyle Anthony Dizon
Author by

Kyle Anthony Dizon

Updated on June 06, 2022

Comments

  • Kyle Anthony Dizon
    Kyle Anthony Dizon about 2 years

    I already followed this. But unfortunately, when I delete a data with the SAME value as the other, it gets deleted as well. I want to be deleting a single data only.

    Here is my database structure. Here is my database.

    I want to be only deleting only a single value.

    Query query = databaseReference.orderByChild("address").equalTo(tvPatientAddress.getText().toString());
    
                            query.addChildEventListener(new ChildEventListener() {
                                @Override
                                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                                    dataSnapshot.getRef().setValue(null);
                                }
    
                                @Override
                                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    
                                }
    
                                @Override
                                public void onChildRemoved(DataSnapshot dataSnapshot) {
    
                                }
    
                                @Override
                                public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    
                                }
    
                                @Override
                                public void onCancelled(DatabaseError databaseError) {
    
                                }
                            });
    

    I have this code

    Any chance you guys have an idea? I think the best way to do this is by getting the ID of the patient and deleting that data, since the ID is unique. Any help appreciated!

  • Kyle Anthony Dizon
    Kyle Anthony Dizon over 6 years
    How will I be getting the UID?
  • Kyle Anthony Dizon
    Kyle Anthony Dizon over 6 years
    How will I get this key? .child("-KwYUqs...key")
  • Atif AbbAsi
    Atif AbbAsi over 6 years
    private String uId = FirebaseAuth.getInstance().getCurrentUser();
  • Kyle Anthony Dizon
    Kyle Anthony Dizon over 6 years
    But I have 2 data with the same "firstname" of "Dummy1", they'll both get deleted?
  • Burhanuddin Rashid
    Burhanuddin Rashid over 6 years
    Yes..it will delete both because you are querying for Dummy1 username
  • Burhanuddin Rashid
    Burhanuddin Rashid over 6 years
    But it will delete in only "91C...key" node
  • Kyle Anthony Dizon
    Kyle Anthony Dizon over 6 years
    Yes, but those are lists of patients, I want to be deleting only one specific child
  • Atif AbbAsi
    Atif AbbAsi over 6 years
    String key = postsnapshot.getKey();use this and get key of object @KyleAnthonyDizon
  • Burhanuddin Rashid
    Burhanuddin Rashid over 6 years
    You need to specify some other attributes too else how will you differentiate between to two Dummy1 patients name
  • Kyle Anthony Dizon
    Kyle Anthony Dizon over 6 years
    I'll try this mate