Delete firebase firestore collections

Asked

Viewed 252 times

0

I have N collections created in my Cloud Firestore, and I would like to delete all of them if possible, as if I were going to reset my database because they were inserted wrong. I wonder if there is how to delete in batch, more than one collection at a time in the firestore.

2 answers

1

by clicking on the three points of the collection you can delete the collection with everything inside it inserir a descrição da imagem aqui

0

I have an angled application with angularfire. I created this way to delete the collection:

deleteCollection(path: string, batchSize: number): Observable<any> {
    const source = this.deleteBatch(path, batchSize);
    return source.pipe(
      expand(val => this.deleteBatch(path, batchSize)),
      takeWhile(val => val > 0)
    );
  }

  /**
   * Em teste!
   * Apaga toda uma coleção.
   */
  private deleteBatch(path: string, batchSize: number): Observable<any> {
    const colRef = this.afs.collection(path, ref =>
      ref.orderBy('__name__').limit(batchSize)
    );
    return colRef.snapshotChanges().pipe(
      take(1),
      mergeMap((snapshot: any) => {
        const batch = this.afs.firestore.batch();
        snapshot.forEach(doc => {
          batch.delete(doc.payload.doc.ref);
        });
        return fromPromise(batch.commit()).pipe(map(() => snapshot.length));
      })
    );
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.