0

I have an array of ids which in my case represent documents inside a collection, my goal is to remove all of those those docs inside a collection according to id's inside an array my question is should I do a for loop on the doc delete function ? or is there a function that allows you to delete multi docs according

  const DeletAccount = async (sharedId: string, clientsIds: string[]) => {
    const batch = writeBatch(db);
    clientsIds.forEach((docId) => {
      batch.delete(doc(db, 'col1', docId));      
    });
    // Commit the batch
    await batch.commit();
    
    // Delete col2
    await deleteDoc(doc(db, 'col2', sharedId));
    // Delete Main doc col3
    await deleteDoc(doc(db, 'col3', currentUser.uid));
    }

const toDelete = ['sdferwer32423asd','Pasdas34234235', 'aMNsdasd21312223232']

to a preference like an array of ids in my case ?

Richardson
  • 1,804
  • 10
  • 38
  • Does this answer your question? [How to delete multiple documents from Cloud Firestore?](https://stackoverflow.com/questions/48175235/how-to-delete-multiple-documents-from-cloud-firestore) – Herohtar Mar 06 '22 at 05:36

1 Answers1

1

There isn't any deleteMany() function but you can try using Batched Writes to delete upto 500 documents in a single batch.

import { writeBatch, doc } from "firebase/firestore"; 

const toDelete = ['sdferwer32423asd','Pasdas34234235', 'aMNsdasd21312223232']

const batch = writeBatch(db);

toDelete.forEach((docId) => {
  batch.delete(doc(db, "collectionName", docId))
}) 

// Commit the batch
await batch.commit();

Alternatively, you can map an array of promises and then use Promise.all() as shown below:

const promises = toDelete.map((docId) => deleteDoc(doc(db, "collection", docId)))

await Promise.all(promises)

The delete() in a batch just requires a DocumentReference so you can delete documents from multiple collections as shown below:

// After forEach in previous code snippet

await batch.delete(doc(db, 'col2', sharedId));
await batch.delete(doc(db, 'col3', currentUser.uid));

// Batch commit at the end
batch.commit();
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • can you combine it with different collections ? I mean if a user has 3 collections and I want to delete the whole user with his 3 collations ? – Richardson Mar 06 '22 at 05:40
  • 1
    @k90-T yes, I have hard coded collection name here but you can add delete docs from multiple collections in a single batch. – Dharmaraj Mar 06 '22 at 05:40
  • this is really good., very helpful. could you please make example how to delete 1 different that is diffrent than the one with array id ? inside the foreach ? – Richardson Mar 06 '22 at 05:43
  • 1
    @k90-T your example in question doesn't provide information on where the collection name will come from. Can you provide more info? Like multiple collections will have different arrays? – Dharmaraj Mar 06 '22 at 05:45
  • I updated my code so you can see the logic – Richardson Mar 06 '22 at 05:45
  • i want to integrate the later 2 delete in to the batch, while still be able to delete the col with multi ids, if this can be done ? – Richardson Mar 06 '22 at 05:48
  • 1
    @k90-T I've updated my answer. Just keep using `batch.delete()` and call `batch.commit()` at the end when you've added all delete ops – Dharmaraj Mar 06 '22 at 05:51
  • so i cannot integrate them in to one ? – Richardson Mar 06 '22 at 05:52
  • 1
    @k90-T it is a single batch... you just have to added them after forEach because they are in another collection. They all will be deleted at once, or won't be delete at all if any one of the operation fails. – Dharmaraj Mar 06 '22 at 05:52
  • so they will be added to the batch – Richardson Mar 06 '22 at 05:53
  • 1
    @k90-T batched writes will ensure that all documents are delete. And you won't end up in a situation where documents from col1 are delete but col2 are not... checkout the documentation linked in my answer. Yes all the docs will be a single batch as in my answer above. – Dharmaraj Mar 06 '22 at 05:54
  • if I understand right , batch will sum all the changes integrate them all tohather then make the chance ideal for my use case when i want to delete the users all related data similar to git commit :D – Richardson Mar 06 '22 at 06:12
  • 1
    Yes, using batch Is ideal here as long as your are deleting less than 500 docs. – Dharmaraj Mar 06 '22 at 06:20