Delete firebase document

Delete Firebase Document

If you are working with Firebase database, there might be some instances where you need to delete a document. There are multiple ways to achieve this based on the type of the document you want to delete.

Deleting a Document Using Document Reference

The first method to delete a document is by using the document reference. You can use the delete() method on the reference to delete the document. Here is an example:

const docRef = firebase.firestore().collection('myCollection').doc('myDocId');

docRef.delete().then(() => {
  console.log("Document deleted successfully");
}).catch((error) => {
  console.error("Error deleting document: ", error);
});

In the above code, we first create a reference to the document we want to delete using the doc() method. We then call the delete() method on the reference. If the document is deleted successfully, we log a success message in the console. If there is an error, we log an error message instead.

Deleting a Document Using Collection Reference

The second method to delete a document is by using the collection reference. You can use the doc() method on the collection reference to create a reference to the document, and then use the delete() method on the reference to delete the document. Here is an example:

const collectionRef = firebase.firestore().collection('myCollection');
const docRef = collectionRef.doc('myDocId');

docRef.delete().then(() => {
  console.log("Document deleted successfully");
}).catch((error) => {
  console.error("Error deleting document: ", error);
});

In the above code, we first create a reference to the collection using the collection() method. We then use the doc() method on the collection reference to create a reference to the document we want to delete. Finally, we call the delete() method on the reference to delete the document.

Deleting a Document Using Batch Write

The third method to delete a document is by using batch write. Batch write allows you to perform multiple operations in a single transaction. Here is an example:

const batch = firebase.firestore().batch();

const docRef1 = firebase.firestore().collection('myCollection').doc('myDocId1');
const docRef2 = firebase.firestore().collection('myCollection').doc('myDocId2');

batch.delete(docRef1);
batch.delete(docRef2);

batch.commit().then(() => {
  console.log("Documents deleted successfully");
}).catch((error) => {
  console.error("Error deleting documents: ", error);
});

In the above code, we first create a batch object using the batch() method. We then create references to the documents we want to delete using the doc() method. We call the delete() method on each document reference, and then call the commit() method on the batch object to perform the deletion. If all documents are deleted successfully, we log a success message in the console. If there is an error, we log an error message instead.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe