How to delete multiple documents that have different ids in firestore?

Viewed 2828

I got a multiple documents with different ids, and i need to implement some method to delete them all, i searched so i guess the way i should go is to use batch. So my database is as shown in this Image

so i was able to get my documents' ids in an array list like that..

[0Y5rfMK3duHBUTN9XsO5, 2Q70mSjNxkAoUMDAJ8rz, etc...]

and my code:

WriteBatch batch = db.batch();
DocumentReference myRef = db.collection("Collection").document(String.valueOf(idsList));
batch.delete(myRef);
batch.commit();

but this doesn't work, so if there is a little missed step, or if there is any other way to perform it, it would be greatly appreciated to write it down.

2 Answers

You will have to iterate your list and create a DocumentReference for each one individually. A DocumentReference can only refer to a single document, not a list of documents:

WriteBatch batch = db.batch();

for (String id : idsList) {
    DocumentReference ref = db.collection("Collection").document(id);
    batch.delete(ref);
}

batch.commit();

Ref : https://firebase.google.com/docs/firestore/manage-data/delete-data

for (String DocId : yourListObject) {
    db.collection("Collection").document(String.valueOf(docID))
            .delete()
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "DocumentSnapshot successfully deleted!");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "Error deleting document", e);
                }
            });
    }

Delete collections

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors. Repeat the process until you've deleted the entire collection or subcollection.

Deleting a collection requires coordinating an unbounded number of individual delete requests. If you need to delete entire collections, do so only from a trusted server environment. While it is possible to delete a collection from a mobile/web client, doing so has negative security and performance implications.

    /** Delete a collection in batches to avoid out-of-memory errors.
 * Batch size may be tuned based on document size (atmost 1MB) and application requirements.
 */
void deleteCollection(CollectionReference collection, int batchSize) {
  try {
    // retrieve a small batch of documents to avoid out-of-memory errors
    ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
    int deleted = 0;
    // future.get() blocks on document retrieval
    List<QueryDocumentSnapshot> documents = future.get().getDocuments();
    for (QueryDocumentSnapshot document : documents) {
      document.getReference().delete();
      ++deleted;
    }
    if (deleted >= batchSize) {
      // retrieve and delete another batch
      deleteCollection(collection, batchSize);
    }
  } catch (Exception e) {
    System.err.println("Error deleting collection : " + e.getMessage());
  }
}

For More Please Visit https://firebase.google.com/docs/firestore/manage-data/delete-data

Related