In my Firebase structure I have a collection and some sub-collections within it. I would like when I delete the collection, also delete the subcollections. I'm trying to do what's in the Firebase documentation: "To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them". But is not working. I delete the collection and the subcollection still saved. My code:
void deleteNestedSubcollections(String id) {
Future<QuerySnapshot> books = libraryCollection.document(id).collection("Books").getDocuments();
books.then((value) => value.documents.remove(value));
Future<QuerySnapshot> catalogues = libraryCollection.document(id).collection("Catalogues").getDocuments();
catalogues.then((value) => value.documents.remove(value));
}
EDIT 1
To delete a document, you need to use the delete
method:
void deleteNestedSubcollections(String id) {
Future<QuerySnapshot> books =
libraryCollection.document(id).collection("Books").getDocuments();
books.then((value) {
value.documents.forEach((element) {
libraryCollection
.document(id)
.collection("Books")
.document(element.documentID)
.delete()
.then((value) => print("success"));
});
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.