Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Deleting document inside a nested Collection

I was making a function to clear all documents inside a nested collection. I've accessed it as follow.

const deleteChat = async () => {

  const chatSnapShot = await db
    .collection("chats")
    .doc(router.query.id)
    .collection("messages")
    .get();

  chatSnapShot.forEach((doc) => {
    console.log(doc.data());
  });
};

How can I do that? This doc.data() returns the data inside the messages collection. I've tried using other solutions but things doesn't work. Help would be appreciated :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The get() reads all documents from that collection. You need to use delete() to delete a document. Try refactoring the code as shown below:

const deleteChat = async () => { 
  const chatSnapShot = await db
    .collection("chats")
    .doc(router.query.id)
    .collection("messages")
    .get();

  const deletePromises = chatSnapshot.docs.map(d => d.ref.delete());

  await Promise.all(deletePromises)
  console.log("Documents deleted")
};

Here chatSnapShot.docs is an array of QueryDocumentSnapshot and you can get DocumentReference for each doc using .ref property. Then the doc reference has .delete() method to delete the document. This method returns a promise so you can map an array of promises and run them at once using Promise.all()


You can also delete documents in batches of up to 500 documents using batched writes.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!