Remove a specific value from Firebase Database
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();
}
Kyle Anthony Dizon
Updated on June 06, 2022Comments
-
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. .
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 over 6 yearsHow will I be getting the UID?
-
Kyle Anthony Dizon over 6 yearsHow will I get this key? .child("-KwYUqs...key")
-
Atif AbbAsi over 6 yearsprivate String uId = FirebaseAuth.getInstance().getCurrentUser();
-
Kyle Anthony Dizon over 6 yearsBut I have 2 data with the same "firstname" of "Dummy1", they'll both get deleted?
-
Burhanuddin Rashid over 6 yearsYes..it will delete both because you are querying for Dummy1 username
-
Burhanuddin Rashid over 6 yearsBut it will delete in only "91C...key" node
-
Kyle Anthony Dizon over 6 yearsYes, but those are lists of patients, I want to be deleting only one specific child
-
Atif AbbAsi over 6 yearsString key = postsnapshot.getKey();use this and get key of object @KyleAnthonyDizon
-
Burhanuddin Rashid over 6 yearsYou need to specify some other attributes too else how will you differentiate between to two
Dummy1
patients name -
Kyle Anthony Dizon over 6 yearsI'll try this mate