'How to delete a document inside a subcollection

I have a collection STUDENTS which has a sub collection PLAYLIST what I want to do is deleting the documents inside the PLAYLIST

this is the code I have tried

    const result = await deleteDoc(doc(db, path, dataId));

the path is "Students/student_id/Playlist" the dataID is the document i wanted to delete but there are no result when i trigger this action



Solution 1:[1]

To delete all the documents inside a sub-collection, you should loop the snapshot and call the deleteDoc() method. See code below:

const querySnapshot = await getDocs(collection(db, "students", "<student_id>", "playlist"));

querySnapshot.forEach((doc) => {
  deleteDoc(doc.ref);
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marc Anthony B