Skip to content
the poor coder

Delete firebase document

Poor Man
Published
Reading time
2 min read
On this page

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:

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

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

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

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC