firebasegoogle-cloud-firestoreangular5angularfire2

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  -->

Solution

  • 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();
    }