0

Connected to the Firestore database using below code,

Query query = dbFirestore.collection("collectionName").whereEqualTo("columnName ==",XXX);
ApiFuture<QuerySnapshot> apiFutureResults = query.get();

From ApiFuture, apiFutureResults.get() ? How to delete the list of documents one by one from here. In my case its only one document for the given condition. I don't see any delete function underneath apiFutureResults .

FYI. This is pure Java Application. No Android. So No listeners please.

aarav
  • 230
  • 1
  • 4
  • 21
  • `ApiFuture` can [have listeners set](https://cloud.google.com/java/docs/reference/api-common/latest/com.google.api.core.ApiFuture), even in pure Java... Without a listener you would need something like await to wait for the task to complete. When the task is complete you can get the QuerySnapshot itself and get document IDs to call delete on. – Tyler V Jun 29 '22 at 03:35
  • Maye this [answer](https://stackoverflow.com/questions/69437683/firestore-or-queries-using-java) will help. – Alex Mamo Jul 06 '22 at 08:35

1 Answers1

0

To delete the list of documents one by one, you can use the delete() function as in the following example:

// [START firestore_data_delete_doc]
// asynchronously delete a document

ApiFuture<WriteResult> writeResult = db.collection("cities").document("DC").delete();

// ...

System.out.println("Update time : " + writeResult.get().getUpdateTime());

// [END firestore_data_delete_doc]

It is important to note that Cloud Firestore does not automatically remove the documents in its subcollections when you delete a document. By reference, you can still access the papers in the subcollection.

A document must be deleted manually along with all of the documents contained in any subcollections.

  • My firebase library dont support a void .document() method on `db.collection("XXX")` could you please share any working resources? I don't find delete method anywhere in *com.google.cloud.firestore.Firestore* class as well. Give me a java solution. – aarav Jun 29 '22 at 18:08
  • Yes, the documentation for the delete method using pure Java code is available [here](https://firebase.google.com/docs/firestore/manage-data/delete-data#java_1). In fact, documentation made it clear that you can use .document() There is additional documentation [here](https://www.hackerheap.com/google-firebase-tutorial-beginners/) that could be useful; in fact, the delete() method is mentioned specifically. You may really find examples (14, 15 and 18) on [this site](https://www.programcreek.com/java-api-examples/?class=com.google.api.core.ApiFuture&method=get) that may be of use to you. – Jose German Perez Sanchez Jun 29 '22 at 21:44