1

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.

Mega_Mere
  • 33
  • 8
  • What is the exact name of the node you want to delete? Provide us an example. – Alex Mamo May 07 '20 at 11:00
  • @AlexMamo sld0000001 and sId0000002 are the ones that i want to delete from surveyList node. I want the child nodes which will be copied from surveyList upon complications to completedSurveys to be deleted from surveyList – Mega_Mere May 07 '20 at 11:09
  • So you only want sld0000001 and sId0000002 to be deleted from `surveyList` node, right? Do you have those values in an array or a list? Would like to be deleted all at once or one by one? – Alex Mamo May 07 '20 at 11:42
  • 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 – Mega_Mere May 07 '20 at 11:50

1 Answers1

1

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);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Dear Alex,I want to remove all child node 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. – Mega_Mere May 07 '20 at 12:09
  • Ok, now I understood exactly. I'll update the answer in a moment. – Alex Mamo May 07 '20 at 12:25
  • Please check my updated answer and tell me if it works. – Alex Mamo May 07 '20 at 12:31
  • The modified code is deleting the child node from both completedSurveys and surveyList. It is the same as the one i posted in the begening. Is there any other way to pass [String surveyId = ds.getKey();] as a string from onDataChange() method. – Mega_Mere May 07 '20 at 13:37
  • The code that I have added deletes only the items in `surveyList`, as you requested. There is **NO** way it deletes items from `completedSurveys`, unless you explicitly specified that in your code. Edit your question with the changed code. – Alex Mamo May 07 '20 at 13:46
  • Hi Alex Mamo, it is still not working. The code is deleting from both surveyList and completedList. – Mega_Mere May 09 '20 at 21:25
  • There is no way it can happen that. Please edit your question with the changed code, to see it more clearly. – Alex Mamo May 09 '20 at 21:45
  • i already have a screen record,if there is a way to post it i would – Mega_Mere May 09 '20 at 22:20
  • There is no need for a screen record, simply edit your question and add the code that produces that behavior. – Alex Mamo May 10 '20 at 08:15
  • 1
    Hi Alex, i actually get an answer from your another post on kind of similar scenario, https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774 Thanks! – Mega_Mere May 19 '20 at 13:24