Twitter LogoFacebook Logo
How to Delete Data in Firestore
Learn how to delete a document in the Firestore database in Angular applications using FirebaseTS
By: King

To delete an existing document in Firestore, first you must add the FirebaseTS library to your project. Follow this tutorial to learn how to add FirebaseTS.

Once you have added FirebaseTS, you are set to go!

Deleting Data

In this section, we will be deleting an document in Firestore using the delete() method from the FirebaseTSFirestore Class.

Here is the definition of the delete() method.

public delete(
    params: {
        path: string [], 
        onComplete?: () => void,
        onFail?: (err: any) => void
    }): Promise<void>;

from - is an string array that points to the location of where to add the data to. 

For example:



The first element of the array points to a collection and the second points to a document and the pattern repeats itself if more elements are added.

onComplete?: () => void - is an optional callback function that allows you to handle logic after the task is completed.

onFail?: err => void - is an optional callback function that allows you to handle logic when the delete operation has failed. 

How to use

1. Import the FirebaseTSFirestore class.

import FirebaseTSFirestore from 'firebasets/firebasetsFirestore/firebaseTSFirestore';

2. Declare and initialize a FirebaseTSFirestore object.

private firestoreFirebaseTSFirestore;

  this.firestore new FirebaseTSFirestore();

3. Call the delete() method.

   path: [

Here, we will delete the document call "User1Document" in the "UserCollection" collection.

Handling code on complete or fail

Sometimes you may want to do something when you have successfully or unsuccessfully deleted a document in Firestore, like a notification message. You can add the optional parameters onComplete and onFail.

   path: [ ... ],
   onComplete: () => {
      // Code gets executed when it was successful.
      alert("Data deleted!");
   onFail: err => {
      // Code gets executed when it fails.

Sign In