user10217645
user10217645

Reputation:

Delete data from firestore

I have add data on firestore Firebase but I don't have specific document because I use addSnapShotListener to retrieve the data. How can I delete the document when I don't know the name of it. Here is the code where I add the data:

mondayCollectionReference.document().set(userMap, SetOptions.merge()).addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Toast.makeText(TimeForm.this, "Submitted", Toast.LENGTH_SHORT).show();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.d("ERROR", e.getMessage());
                                }
                            });

Upvotes: 0

Views: 371

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

document() returns a DocumentReference object. That object gives you everything you need to know to delete it, especially its own delete() method. It also has a getId() method to help you remember its id.

So, you should store the DocumentReference object first before calling methods on it:

DocumentReference ref = mondayCollectionReference.document()
String id = ref.getId();
ref.set(...);
// use ref or id later if you want to delete it

Upvotes: 1

Related Questions