3

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 Answers2

5

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();
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you, but there is another issue, this is works until i make the action of delete, so i put that code inside onClickListener for a button, if i didn't click the button yet it won't delete, and if i press the button it will delete, but once i enter a new data it instantly deleted without pressing the button again. – Abdelrhman Ahmed Sep 08 '18 at 17:50
  • 1
    It sounds like you should ask another question that explains what isn't working the way you expect. – Doug Stevenson Sep 08 '18 at 17:59
0

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

Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
  • I tried that before and it didn't work for me, it logged DocumentSnapshot successfully deleted!, but the problem that i pass an array of ids, and that way need to pass a separate document id to to actually delete it – Abdelrhman Ahmed Sep 08 '18 at 17:19
  • i edited my answer please check – Ashvin solanki Sep 08 '18 at 17:23
  • Thank you, but there is another issue, this is works until i make the action of delete, so i put that code inside onClickListener for a button, if i didn't click the button yet it won't delete, and if i press the button it will delete, but once i enter a new data it instantly deleted without pressing the button again. – Abdelrhman Ahmed Sep 08 '18 at 17:51
  • @AbdelrhmanAhmed ??? but once i enter a new data it instantly deleted without pressing the button again – Ashvin solanki Sep 08 '18 at 17:57