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

Tags: , , ,



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) {

        }
    });

enter image description here

Attached below is the firebase realtime database structure.

Answer

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);


Source: stackoverflow