I want to delete only the 1st child (L7jrJ6DtQWrmZsC4zvT
) from Firebase database with an option in an Android app. I searched several places and could not find it. You only have one option to delete a whole database. Can anyone help?
Asked
Active
Viewed 820 times
0

Alex Mamo
- 130,605
- 17
- 163
- 193

Rafael Said
- 19
- 3
2 Answers
1
To delete a single object you can use removeValue()
method directly on the reference like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("calendario").child("-L7jrJ6DtQWrmZsC4zvT").removeValue();

Alex Mamo
- 130,605
- 17
- 163
- 193
0
If i understood you correctly, you have the key of the child you want to remove and this child has no parent. so you can just use the single event listener like this -
val ref = FirebaseDatabase.getInstance().reference
val applesQuery = ref.child("L7jrJ6DtQWrmZsC4zvT")
applesQuery.addListenerForSingleValueEvent(
object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
dataSnapshot.ref.removeValue()
}
override fun onCancelled(databaseError: DatabaseError) {
Log.e("frgrgr", "onCancelled", databaseError.toException())
}
})
this should work, let me know otherwise.

Itay Feldman
- 846
- 10
- 23
-
1You don't need a listener for this, you can just call `removeValue()` on the ref you want to delete. – André Kool Mar 19 '18 at 11:24
-
Thanks, but if the user don't know the child (L7jrJ6DtQWrmZsC4zvT)? The users have a list in app and select an item in list to delete... – Rafael Said Mar 19 '18 at 11:25
-
Because in this example he has a specific key with out parents and other children to deal with, your right it can be done shortly like you mentioned – Itay Feldman Mar 19 '18 at 11:27
-
which specific value in your picture you need to delete? – Itay Feldman Mar 19 '18 at 11:44