0

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?

Firebase

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

2 Answers2

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