Categories
JavaScript Answers

How to delete document from Firestore using where clause with JavaScript?

Spread the love

Sometimes, we want to delete document from Firestore using where clause with JavaScript.

In this article, we’ll look at how to delete document from Firestore using where clause with JavaScript.

How to delete document from Firestore using where clause with JavaScript?

To delete document from Firestore using where clause with JavaScript, we can use the delete method.

For instance, we write

const jobSkills = await store
  .collection("job_skills")
  .where("job_id", "==", post.job_id)
  .get();

const batch = store.batch();

jobSkills.forEach((doc) => {
  batch.delete(doc.ref);
});

await batch.commit();

to get the entries we want to delete with where and get.

We call batch to catch the delete operations.

Then we call forEach with a callback that calls batch.delete to delete the entry by referencing it with doc.ref.

Next we call batch.commit to commit the delete operations.

Conclusion

To delete document from Firestore using where clause with JavaScript, we can use the delete method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *