1

I am new at flutter with firebase so if this feels like a question which has been answered in the docs or by other questions, I apologize!!

Now I am saving user data in firestore and when a user deletes their account, I want that to be deleted too but the Firestore docs says that the only way to delete a collection us:

To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them.

This is not recommended for native devs but I thought of an idea:

So firestore overwrites new data against the old one, so if we set a new collection (user-root) which has a single doc with no fields and then delete it, it would be read/write effective as it just overwrites all of the preexisting data.

I have not tried this idea out but if anyone has a better approach to delete collections in firestore, please write an answer!

Reference Links:

Mood Board
  • 59
  • 3
  • 7

1 Answers1

4

It's not possible to "set a new collection". You can only read, write, and query individual documents within a collection.

The advice you quoted here about deleting a collection is absolutely true - you must query, iterate, and delete each document individually. There are no workarounds for this for web and mobile clients.

If you wish to add a backend to your app, you could use the firebase-tools library in a callable Cloud Function to make this process easier, but that library still follows this process to delete all documents in a collection.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • so I have users start their collections at the root, and for each user there are 4 required documents which have many sub-collections and documents, so when I query those 4 docs, will I be reading all the sub-collections as well? because that would be too expensive for me in terms of the toal READ (s)? – Mood Board Aug 10 '20 at 04:39
  • No, queries are shallow and only read documents in the immediate collection. You have to query subcollections separately if you want those documents also. – Doug Stevenson Aug 10 '20 at 04:46
  • So if I don't query the sub-collections (to delete them), would they be deleted or move up to the root collection? – Mood Board Aug 10 '20 at 04:49
  • No, deletes are shallow too. All of this stuff you can try and see for yourself how it works. – Doug Stevenson Aug 10 '20 at 05:17