1

How to delete a document that has a subdocument in firestore database?

Here is the collection structure

enter image description here enter image description here I would like to delete the document doc("3F0SezA3")

I tried these below but none had work, i am getting console.log('deleted') but in the database the document is still showing

firebase.firestore()
    .collection("users")
    .doc("3F0SezA3").delete()
    .then(function () {
                console.log('deleted');
              }, function (error) {
                console.error('Delete Error', error);
              });
 firebase.firestore()
.collection("users").doc("3F0SezA3")
.collection("movives").doc("asdrftgyqweAsdghj120a").delete()
.then(function () {
                console.log('deleted');
              }, function (error) {
                console.error('Delete Error', error);
              });
10
  • 2
    What are are you seeing that suggests the document is not deleted? Did you check for errors on each delete? Please edit the question to explain in more detail. Commented Jun 14, 2020 at 5:21
  • no error is being thrown noting is happening @DougStevenson Commented Jun 14, 2020 at 5:57
  • 1
    Your code doesn't show any error handling being done with the promises returned by delete(). Commented Jun 14, 2020 at 6:16
  • Could you include the structure of your db? Commented Jun 14, 2020 at 7:05
  • What do you exactly mean by "How to delete a document that has a subdocument in firestore database?" You want to select the subdocument and delete the parent? OR, you want to delete the parent and THEN delete the subdocument? Commented Jun 14, 2020 at 17:01

1 Answer 1

2

If you want to delete the two documents at the same time, the best is to use a batched write, as follows:

var batch = firebase.firestore().batch();

var parentRef = firebase.firestore()
    .collection("users")
    .doc("3F0SezA3");
batch.delete(parentRef);

var childRef = parentRef
    .collection("movives")
    .doc("asdrftgyqweAsdghj120a");
batch.delete(childRef);

batch.commit()
.then(function () {
    console.log("Docs deleted")
})
.catch(function (error) {
    console.log(error)
});
Sign up to request clarification or add additional context in comments.

7 Comments

hi @RenaudTarnec how can i delete this part firebase.firestore().collection("matches").doc("3F0SezA3"); alone, i tried this firebase.firestore().collection("matches").doc("3F0SezA3").delete() .then(function () { console.log("Docs deleted") }) .catch(function (error) { console.log(error) }); but it is not working
Hi, what you tried should normally work. Do you get any error.
ok what i am trying to get at is how to delete a document with aboout over 100 sub document, the sub documents all have dynamic ids @RenaudTarnec
Since queries in Firestore are shallow, you need to query all the sub-documents in the sub-collection of this Document and delete them. See also this: firebase.google.com/docs/firestore/solutions/delete-collections
thanks, could you please show me what path i would have to use function deleteAtPath(path) {} to initiate the server-side delete @RenaudTarnec
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.