New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Official version to delete a collection and all nested documents #1400
Comments
You have to do it manually: loop over every subcollection and delete each document. |
Do you have a sample you can share? |
Yeah, the official docs have an example: https://firebase.google.com/docs/firestore/manage-data/delete-data |
@larssn thank you for the link. However, that is the plain vanilla version of Delete. I was wondering if there is an AngularFirestore version written in Typescript |
There's no angularfire version. You'd have to access the native Firestore SDK references:
The above example is off the top of my head, so it might contain errors, but the principle is there. |
@larssn you're awesome!!! Thank you very much. I'll give it a try and update this ticket with findings |
Hi @gigocabrera! I would love this feature as well. However, AngularFire is a wrapper around the core SDK. Route any future feature requests to the official SDK repo. They are much wiser than me :) |
Thank you @davideast 🍺 |
Hi all, Sharing a function to delete all documents in named collections, by batches. It returns a promise with total number of deleted documents. It does not delete subcollections. Internally, uses rxjs to do the magic of streaming and batching values to be deleted. /**
* Delete all documents in specified collections.
*
* @param {string} collections Collection names
* @return {Promise<number>} Total number of documents deleted (from all collections)
*/
async deleteCollections (...collections: string[]) {
let totalDeleteCount = 0
const batchSize = 500
return new Promise<number>((resolve, reject) => Observable
.from(collections)
.concatMap(collection => Observable.fromPromise(this.fireStore.collection(collection).ref.get()))
.concatMap(q => Observable.from(q.docs))
.bufferCount(batchSize)
.concatMap((docs: QueryDocumentSnapshot[]) => Observable.create((o: Observer<number>) => {
const batch = this.fireStore.firestore.batch()
docs.forEach(doc => batch.delete(doc.ref))
batch.commit()
.then(() => {
o.next(docs.length)
o.complete()
})
.catch(e => o.error(e))
})
)
.subscribe(
(batchDeleteCount: number) => totalDeleteCount += batchDeleteCount,
e => reject(e),
() => resolve(totalDeleteCount)
)
)
} |
@kctang that's awesome, thank you! I'll give it a try this weekend |
Thanks @kctang works like a charm. |
here's the function from the docs (node.js but works as is in javascript if you replace process.nextTick by setTimeout 0) |
I used this as a workaround to delete a user's Collection from the Android client. Passed into the method is the reference of the user's Collection and the batch size to process. I will refactor this onto the server as recommended by the documentation, but am using this for my proof of concept in the meantime.
|
The solution here can be easily adapted for frontend too: firebase/firebase-admin-node#361 |
I needed to delete a collection inside another collection, which @kctang solution wasn't working for me, so I changed it to this and using rxjs6 operators import { AngularFirestore, AngularFirestoreCollection, QueryDocumentSnapshot } from '@angular/fire/firestore';
import { from, Observable, Observer } from 'rxjs';
import { bufferCount, concatMap } from 'rxjs/operators';
async deleteCollection(collection: AngularFirestoreCollection<any>): Promise<number> {
let totalDeleteCount = 0;
const batchSize = 500;
return new Promise<number>((resolve, reject) =>
from(collection.ref.get())
.pipe(
concatMap((q) => from(q.docs)),
bufferCount(batchSize),
concatMap((docs: Array<QueryDocumentSnapshot<any>>) => new Observable((o: Observer<number>) => {
const batch = this.fireStore.firestore.batch();
docs.forEach((doc) => batch.delete(doc.ref));
batch.commit()
.then(() => {
o.next(docs.length);
o.complete();
})
.catch((e) => o.error(e));
})),
)
.subscribe(
(batchDeleteCount: number) => totalDeleteCount += batchDeleteCount,
(e) => reject(e),
() => resolve(totalDeleteCount),
),
);
} Then you can pass a collection reference, which can be a nested collection |
This still doesn't serve our purpose . it will delete all documents within collection but not collection itself. |
The collection is just an abstraction, it doesn't really exist. So it is "deleted" when all it's documents are removed. |
Would anyone be able to share a Python version of this? |
Feature Request
Can you include in the Cloud Firestore documentation an official version on how to delete a collection and all nested documents please
For example, if I have the following data structure in Firestore
-- how can I delete a user and all the forms?
users
---userId
-----forms
--------formId
The text was updated successfully, but these errors were encountered: