Lionlollipop
Lionlollipop

Reputation: 123

Get and delete all documents in a Firestore subcollection

I want to get and delete all the documents in a subcollection called "journal" but it doesn't work and did not show any toast message or errors.

It would be very helpful if someone tells me what is wrong. Thank you a lot.

This is the code I use to get and delete all the documents in a subcollection:

final String userid = FirebaseAuth.getInstance.getCurrentUser().getUid();

    FirebaseFirestore. getInstance().collection("main").document(userid).collection("journal").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (final QueryDocumentSnapshot document : task.getResult()) {

 db.collection("main").document(userid).collection("journal").document(document.getId()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(SettingsActivity.this, "Deleted: " + document.getId(), Toast.LENGTH_SHORT).show();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(SettingsActivity.this, "error getting: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

Firestore structure :

collection(main) ---> document(userid)---> collection(journal)---> document 1
                                                              ---> document 2
                                                              --> document 3

enter image description here

Upvotes: 0

Views: 65

Answers (1)

elbert rivas
elbert rivas

Reputation: 1464

Try this.

final String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();

FirebaseFirestore.getInstance().collection("main").document(userid).collection("journal").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        for (DocumentSnapshot document: queryDocumentSnapshots.getDocuments()) {
            db.collection("main").document(userid).collection("journal").document(document.getId()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(SettingsActivity.this, "Deleted: " + document.getId(), Toast.LENGTH_SHORT).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }
    }
});

Also, please make sure you have set your db rules to allow delete event

Upvotes: 1

Related Questions