How to Remove Child Nodes in Firebase Android

How to remove child nodes in firebase android?

To remove data:

firebase.child(id).removeValue();

You might do well to have a look at the Firebase documentation for Android btw, which covers this and many more topics.

How to delete the individual childs from firebase in android?

When you use push(), you are generating a new random id, then when you use removeValue() on that key it will lead to deleting that key.

If you want to delete an existing key in your database, then you need to retrieve it from the database to be able to use it with removeValue().

How can i delete firebase child node with the same name?Get the key to be delited from one node and delete those node with same key from the other

It could be deleted one by one or once; I just want to delete all the names which are found on both DB from surveyList.

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference completedSurveysRef = rootRef.child("users").child(uid).child("completedSurveys");

Now to remove all values within the completedSurveys node at once, you should use the following line of code:

completedSurveysRef.removeValue();

If you want to delete each child separately, please use the following lines of code:

completedSurveysRef.child("sld0000001").removeValue();
completedSurveysRef.child("sld0000002").removeValue();

Edit:

According to your comment:

Dear Alex, I want to remove all child nodes from surveyList, that are in completedList. sld0000001 and sld0000002 are both in completedList and surveyList, so I want to remove these values from surveyList not from comletedList.

Please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference completedSurveysRef = rootRef.child("users").child(uid).child("completedSurveys");
DatabaseReference surveyListRef = rootRef.child("users").child(uid).child("surveyList");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String surveyId = ds.getKey();;
surveyListRef.child(surveyId).removeValue();
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
completedSurveysRef.addListenerForSingleValueEvent(valueEventListener);

How to remove specific nodes in firebase real time database

First, As per your comment you need to get autogenerated key.For that :-

  public String keyval;

FirebaseDatabase.getInstance().getReference().child("numbers-guess-...").child("simnumbers").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

if (dataSnapshot != null && dataSnapshot.getValue() != null) {

// for (DataSnapshot child : dataSnapshot.getChildren()) {
// if we want to get do operation in multiple data then write your code here
// }

keyval = dataSnapshot.getKey());
}

}

@Override
public void onCancelled(DatabaseError databaseError) {
//add code in case you not get proper dat from firebase
}
});

To remove value in firbase you need to use removeValue() and as per my view you should use it with addOnCompleteListener().

Now, add that keyval as a key which you want to remove. show below code:-

FirebaseDatabase.getInstance().getReference()
.child("simnumbers").child(keyval).removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
//enter your code what you want excute after remove value in firebase.
} else {
//enter msg or enter your code which you want to show in case of value is not remove properly or removed failed.

Toast.makeText(this, "Remove Failed", Toast.LENGTH_SHORT).show();
}
}
});

Can't remove child from firebase

In addToDoElement you initialize mTasksDatabaseReference to:

mTasksDatabaseReference = mFirebaseDatabase.getReference().child(email).child("tasks").child(title);

You then use mTasksDatabaseReference in deleteTask to delete the child.

mTasksDatabaseReference.child(email).child("tasks").child(childToDelete).setValue(null);

But since you already set mTasksDatabaseReference as a reference to the specific task, you're now trying to point to a task under another task.

public void deleteTask(View view) {
TextView itemTitle = findViewById(R.id.task_title);
String childToDelete = itemTitle.getText().toString();
Toast.makeText(getApplicationContext(), "Deleted task: " + childToDelete, Toast.LENGTH_SHORT).show();
FirebaseDatabase.getInstance().getReference()
.child(email).child("tasks").child(childToDelete).setValue(null);
}


Related Topics



Leave a reply



Submit