0

I have created the following structure in my firestore:

/Users/USERID/Chats/UNKNOWN_DOCUMENT_ID/isClicked/isClicked-> Field: isClicked = 0/1

Which is:

/collection/document/collection/document/colection/document/Field

Now, I want to add an option for users to delete their account which also means to delete the path above. However, I cant figure out how to delete it.

I tried to do the following:

db.collection( "Users" ).document(auth.getUid()).collection("Chats").get()
        .addOnCompleteListener( task -> {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    if (document != null) {
                        db.collection( "Users" ).document(auth.getUid()).collection("Chats").document(document.getId()).collection("isClicked").document("isClicked").delete();
                    }
                }
            }
        } );

But I keep getting that task.getResult size is 0 and there is no document even though that there is a document for sure but I don't know its ID.

Any solution to how to delete it?

I read that it is not recommended to delete the collection from within the code but still.

Thank you

Ben
  • 1,737
  • 2
  • 30
  • 61

1 Answers1

0

You probably didn't actually write any documents under "/Users/USERID/Chats/". If that's the case, then your query will return nothing. Creating a subcollection under a document ID will not automatically create that document.

Look at the Firesotre console for that document ID. Is it in italics? If so, there is not actually a document there. It is just a "phantom document" placeholder that lets you click into the subcollection. These documents can't be queried, nor can they be deleted. You would have to delete all of its nested subcollection in order to see it removed from the console.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • It indeed shows the ID as Italics. Probably deletes the parent of this path. Thank you. – Ben Oct 21 '20 at 20:16