8

I'd like to remove a document from Firebase sub-collection. I'm trying to do this in the following way:

firestore.collection('categories').doc(categoryId).collection('books').doc(bookId).delete();

And it doesn't work.

However, I'm able to remove a document from the collection:

firestore.collection('categories').doc(categoryId).delete();

Am I losing sight of something? How should it work?

UPDATED:

const firebase = require('../firebase/firebaseAdmin');
const firestore = firebase.firestore();

module.exports = {
  removeBookFromCategory: (categoryId, bookId) => (
    firestore
      .collection('categories')
      .doc(categoryId)
      .collection('books')
      .doc(bookId)
      .delete()
  ),
};

I have correct ids here but I'm getting 500 error:

Error: Argument "documentPath" is not a valid ResourcePath. Path must be a non-empty string.

4
  • Could you edit the question to show the exact contents of your data, the values of the variables you're using, and the result of the promise that's returned by delete() (does it resolve with an error?). Commented Feb 9, 2018 at 15:08
  • There should be no problem to do this Commented Feb 9, 2018 at 15:09
  • There could be a problem if your delete is not allowed by security rules or failure to authenticate. Please update your question with what I asked earlier, especially the results of the promise returned by delete(). Commented Feb 9, 2018 at 15:45
  • Ah, my problem was the wrong endpoint! I had: router.delete('/categories/:id/books/:id/remove', removeBook); instead of router.delete('/categories/:categoryId/books/:bookId/remove', removeBook); Commented Feb 9, 2018 at 18:56

2 Answers 2

7

When you delete a document that has associated subcollections, the subcollections are not deleted. They are still accessible by reference. When you execute,

firestore.collection('categories').doc(categoryId).collection('books').doc(bookId).delete();

it will delete the document. But it won't delete subcollections. If you want to delete subcollections it has to be done manually. Please refer here

Sign up to request clarification or add additional context in comments.

2 Comments

I'd like to be able to delete the only document from subcollection.
It's an important issue as well. Thanks for turning attention to this.
2

I may be answering this too late, but i figured it out. If your database has the following scheme: rootCollection -> document -> subcollection -> documentInsideSubCollection

you can delete a document inside a subcollection using the snippet below:

  firestore.collection(rootCollection)
  .doc(documentName)
  .collection(subCollectionName).ref  
  .where(field, "==", something)
  .onSnapshot(snapshot => snapshot.forEach(result => result.ref.delete()));

Comments

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.