I want to delete all the child nodes form different nodes with the same names. I used the following code to get the keys of the child nodes to be deleted and pass it to the other node but it is deleting them from both nodes(completedSurveys
and surveyList
).
DatabaseReference dr_completedSurveys = firebaseDatabase.getReference("users").child(userId).child("completedSurveys");
dr_completedSurveys.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
StringBuilder stringBuilder = new StringBuilder();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
stringBuilder.append(ds.getKey()).append("\n");
String completedSurveys = stringBuilder.toString();
String[] lines = completedSurveys.split("\n");
for (String line : lines) {
//Toast.makeText(HomePage.this, line, Toast.LENGTH_SHORT).show();
String compSurList = line;
DatabaseReference dr_completed = FirebaseDatabase.getInstance().getReference("users").child(userId);
dr_completed.child("surveyList").child(compSurList).removeValue();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Attached below is the firebase realtime database structure.