How to delete document from firestore using where clause

Amit Sinha picture Amit Sinha · Nov 8, 2017 · Viewed 42.1k times · Source
var jobskill_ref = db.collection('job_skills').where('job_id','==',post.job_id);
jobskill_ref.delete();

Error thrown

jobskill_ref.delete is not a function

Answer

Frank van Puffelen picture Frank van Puffelen · Nov 8, 2017

You can only delete a document once you have a DocumentReference to it. To get that you must first execute the query, then loop over the QuerySnapshot and finally delete each DocumentSnapshot based on its ref.

var jobskill_query = db.collection('job_skills').where('job_id','==',post.job_id);
jobskill_query.get().then(function(querySnapshot) {
  querySnapshot.forEach(function(doc) {
    doc.ref.delete();
  });
});