简体   繁体   中英

How to delete multiple documents from Cloud Firestore using Batched writes in angular 5

as referenced in firebase cloud firestore Batched writes:

If you do not need to read any documents in your operation set, you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.

how to delete all documents in a collection in using angular 5?

// Get a new write batch
var batch = db.batch();


// Delete the city 'LA'
var laRef = db.collection("cities").doc("LA");
batch.delete(laRef);



<!--  Delete All Tasks  -->
    <button 
      mat-button mat-icon-button 
      (click)="tasksService.deleteAllTasks(openSnackBar())" 
      matTooltip="Delete All Tasks" 
      class="toolbar-btn" 
      [disabled]="(tasksService.tasks$ | async)?.length == 0">
      <i class="fa fa-trash"></i>
    </button>
<!-- Delete All Tasks  -->

It's so easy, try this!

private async deleteCollection(path: string) {
    const batch = db.batch();
    const qs = await db.collection(path).ref.get();
    qs.forEach(doc => batch.delete(doc.ref));
    return batch.commit();
}

I had to add firestore.batch() to the solution our coturiv friend to make it work. this is my first contribution.

private async deleteCollection(path: string) {
        const batch = db.firestore.batch();
        const qs = await db.collection(path).ref.get();
        qs.forEach(doc => batch.delete(doc.ref));
        return batch.commit();
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM