0

I have the following batch, where I delete three documents in a collection, the data-set-c collection however has one nested collection for massages where each message is a doc each.

the problem is that the (data-set-c) collection never gets deleted, I don't know if it is due to the nesting taking place? will deleting this way affect also sub-collection? or is it the rules that are blocking it since my rules are specific to the deepest level endpoint, or should use the cloud function instead since this doc will be massive later on, and each day is massages collection but the main issue here is how to delete this one level nested structure.

could you please take a look and see what I am doing wrong.

// Batch
      const batch = writeBatch(db);
      
      // data-set-a/{Id}/sub-set-a/{subSetId} ---> the direct document
      const colA = collection(db, data-set-a,_authContext.currentUser.uid, sub-set-a);
      
      // data-set-b/{Id}/sub-set-b/{subSetId} ---> the direct document
      const colB = collection(db, data-set-b, _authContext.currentUser.uid, sub-set-b);
      
       // data-set-c/{Id}/sub-set-c/{subSetId}/masseges/{mgsId} /*nested collection*/ 
      const colC = collection(db, 'data-set-c', _authContext.currentUser.uid, sub-set-c);
      
      const docA = doc(colA, subSetId);
      const docB = doc(colB, subSetId);
      const docC = doc(colC, subSetId);
      const docsArr = [docA, docB, docC];

      docsArr.forEach((col: any) => {
      batch.delete(col)
      });
      await batch.commit();

// sub-set-a + sub-set-b SECURITY RULES 
  match /data-set-a/Id}sub-set-a/{subId} {
  allow delete: if request.auth != null 
  && request.auth.token.email_verified
  && request.auth.uid  == Id  
  }    

// sub-set-c SECURITY RULES 
   match /data-set-c/{Id}/sub-set-c/{subSetId}/masseges/{mgsId} {
   allow delete: if request.auth != null 
   && request.auth.token.email_verified
   && request.auth.uid  == Id
  }

enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Richardson
  • 1,804
  • 10
  • 38
  • 2
    As far as I can tell, your code doesn't "triple delete three collections". It simply deletes three documents in a batch. We can't see which documents are being deleted, since all your data is hidden behind variables whose values we can't see. We also can't see the error message, nor can we know which document that refers to. Please edit the question to be clear and specific about what is happening that's different than what you expect. – Doug Stevenson Jun 16 '22 at 03:54
  • @DougStevenson I did refactor the question, sorry for that. – Richardson Jun 16 '22 at 04:19

1 Answers1

2

the problem is that the (data-set-c) collection never gets deleted.

That's the expected behavior. If you delete a document, doesn't mean that you delete all sub-collections that exist within that document. Besides that, that document ID will continue to exist and the Firebase Console will display it in italic.

will deleting this way affect also sub-collection?

No, it won't.

or is it the rules that are blocking it since my rules are specific to the deepest level endpoint.

If it was about the security rules, then you would have received an error indicating that.

or should use cloud function instead since this doc will be massive later on

Yes, you can indeed use a Cloud Function to delete all documents that exist in all sub-collection of the document that was deleted.

How to delete this one-level nested structure.

You can achieve this by iterating each sub-collection, and by deleting each document. But don't do it on the client!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for detailed answer, what is the syntax for doing such delete then , could you please show it in code ?. – Richardson Jun 16 '22 at 16:11
  • Should I delete at deep level then move up ? And repeat depending on how deep the thing is? – Richardson Jun 16 '22 at 16:13
  • I other words , does firebase offer a one liner solution that will delete main doc and it’s sub collections or it should be done manually and after that you delete the father doc. – Richardson Jun 16 '22 at 16:19
  • 1
    No, Firebase doesn't offer that. It should be done manually. Right after you delete all documents within the sub-collection, the "parent" document will disappear too. – Alex Mamo Jun 16 '22 at 19:02
  • I had to admit I found this following on callable function https://firebase.google.com/docs/firestore/solutions/delete-collections in the docs that in my case serves me really well , do you think it might cause any memory implications ? or can I use it as an alternative to your current solution ? – Richardson Jun 17 '22 at 02:10
  • That resource describes how to use a callable Cloud Function to delete data. Once you deploy this function, you can call it directly from your website to recursively delete documents and **collections**. – Alex Mamo Jun 17 '22 at 06:38