What happens to documents and subcollections in a deleted collection? - firebase

Let's say that I have a collection with 5 documents and I delete that collection directly from the Firebase Console... do all the documents and nested collections are deleted too?
If the answers is yes... this behavior is only when doing it directly from the Console? Or is it possible from the SDK?

I delete that collection directly from the Firebase Console... do all the documents and nested collections are deleted too?
YES. All their documents and its subcollections will get removed in a click. Please refer the doc here!
this behaviour is only when doing it directly from the Console?
You could do it using Firebase SDK as well, like mentioned below.
is it possible from the SDK?
Yes it is possible; but, you cannot achieve it with single query call. To delete an entire collection or subcollection in Firestore, retrieve all the documents within the collection or subcollection and delete them.
Firestore ref - https://cloud.google.com/firestore/docs/manage-data/delete-data#collections

Yes if you delete a collection all of its documents and nested collection will get deleted. Also, the deletion can be done directly in the console and from the SDK

Related

Does Firebase remove collection indexes when a collection becomes empty?

When a collection is deleted from Cloud Firestore, its indexes are deleted along with it. I presume that when a collection goes from one or more documents to zero documents that its indexes are preserved. However, in the Cloud Firestore UI, when a collection goes from one document to zero the collection disappears from the root collections tree. Again, I presume this is an artifact of the Cloud Firestore UI, but it got me wondering whether something more happens when a collection becomes empty (as opposed to the collection being deleted outright).
Can you please help clarify what happens (if anything) when a collection goes from one or more documents to zero in Cloud Firestore? Do I need to be worried about losing any indexes when this occurs?
I'm neither Googler nor Firebaser, BUT...
Firestore indexes documents, not collections - the collection paths are an organizing principle more than physical entities. The "collections" are part of the path to documents, and it's the paths and the document fields that end up indexed.
Case in point: you can actually delete a collection while child documents remain, and they will still be indexed with the collection name/ID as part of their path - you'll see this in the console with the collection (and any interstitial document) names italicized.
When a collection goes from 1 to 0 documents, all that happens is that the document is gone, and nothing else. The UI sees no reason to display a collection when there is nothing to show.
Collections don't really "exist". They are just ways to organize documents for the purpose of making queries. What you see in the console is just there to help you visualize the contents of the database. Collections will apparently spring into "existence" when a document is first created, and just as quickly disappear when there are none. They do not work like directories in a filesystem.
An index is just a way of telling Firestore that you have special query needs for documents in a certain named collection or collection group. The index simply enables the query against the documents in the collection or collection group that you name. The index works without requiring any documents to index, and it will continue working no matter how many documents exist.
Some great answers by LeadDreamer and Doug already, but one more thing you seem to be curious about: deleting all documents from a collection does not affect the index definitions for that collection. So if you later add documents to the collection again, the same index definitions will still apply.

How to find Firestore collections which don't have parent documents?

I'm trying to clean up my Google Cloud Firestore database, and I have some subcollections with no parent doc (the parent was deleted). How can I find all of those, using the Firebase Admin SDK, so I can delete them?
You will end up writing a lot of code for this. I'm going to link to nodejs APIs.
For each collection where there could be missing documents, you will need to need to query that collection with listDocuments(). That will return a list of all documents in the collection, including the missing documents that have subcollections. You will then need to iterate the DocumentReferences returned in that list, and call get() on every one of them. The returned DocumentSnapshot will then tell you if it exists or not using its exists property.
After you have all the DocumentReference objects referring to missing documents, you can then follow the instructions in this other question that describes how to delete all nested subcollections under that DocumentReference, go straight to the Firebase documentation.

Firebase Firestore Read Count?

Firebase Firestore costs based on number of read operations. If I download a higher level document that has more then one sub-documents ( like Downloading a parent node that has more than one child node in Firebase Realtime Database.) then will it be considered as a single read or multiple read? I have not found any point about this in the documentation. Please explain?
Firestore queries are always shallow, and do not consider documents in subcollection. The only way to query for documents in subcollecitons is to target that subcollection with a separate query.
It does not work like Realitme Database, which gives you all child nodes with a parent node.

Is it possible to check if a document has a subcollection in firestore?

I want to write a recursive function in which I can pass in a document reference and delete that document and it's subcollections. I was thinking that the base case would be that the document has no subcollections. If it has a subcollection, I would pass that subcollection into the recursive function, and delete any existing documents. If not, I would simply delete that document and return. So is there someway I can check if a document has a subcollection, and if so retrieve it?
According to Doug Stevenson here there are no client SDK APIs for listing subcollections of a document.
Although you may find your answer here but that also may create further problems. In documentation also deleting collections from an Android client is not recommended.

Create multiple documents in Cloud Firestore at the same time

Is it possible to create multiple documents against a collection using Cloud Firestore in the same transaction?
I'm looking at the documentation on batched writes. Unless I'm mistaken (I'm new to Firebase, so could be the case) these examples are meant to demonstrate 'batched writes' but the examples only show a single field being updated.
Yes. Batched writes can work for both updating a single field on multiple documents, or creating a bunch of documents at once.
Follow this link to see a similar case i had which i solved. It talked about a collection with its documents being automatically created when a document is created. You will get an idea on how to solve your issue from it.
The link is thus: When collection is created, the documents are added to it at once
I hope it helps.

Resources