How to delete a specific Child in Firebase?

Asked

Viewed 1,479 times

0

I want to delete only 1st Child (L7jrj6dtqwrmzsc4zvt) from Firebase through an option in an Android app. I searched several places and could not find. I only found the option to delete the whole database. Can anyone help?

inserir a descrição da imagem aqui

  • 2

    If possible post your code. Already tried rootRef.child("L7jrJ6DtQWrmZsC4zvT").removeValue();?

  • The example shows a game calendar. I know which Child is because I have access to the Firebase console. However, the person responsible for scheduling the games does not have access to the console, so he has no way of knowing Child. I wanted him to have the option to delete a game from the list from a selection in the app.

2 answers

1

I got it with the following code:

Query queryRemoverCalendario = mCalendarioDatabaseReference.limitToFirst(1);                    

queryRemoverCalendario.addListenerForSingleValueEvent(new ValueEventListener() {

@Override
 public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot removerCalendarioSnapshot : dataSnapshot.getChildren()) {
removerCalendarioSnapshot.getRef().removeValue();
 }
}
@Override
public void onCancelled(DatabaseError databaseError) {
 }
});

1

You will need to read and then delete. Use limitToFirst(), passing the value 1 to catch only the first Child:

calendarioRef.limitToFirst(1).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String childKey = dataSnapshot.getKey();
                calendarioRef.child(childKey).removeValue();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
  • Rosario, thank you so much for your help. I got what I wanted.

  • Glad you could. It would be nice if you could post the solution you got as an answer. It will help others who have the same problem :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.