3

As in title. I have a String shoppingListId which holds current clicked in RecyclerView documentID and I wonder how to delete this selected document ID.

I tried the following one but it doesn't works because of Incompatible types:

FirebaseFirestore docRef = FirebaseFirestore.getInstance();
DocumentReference selectedDoc = docRef.collection("products").document(shoppingListId);
selectedDoc.delete();

How to get Instance to DocumentReference, and then delete selected document? I guess this is probably so easy, but Im stuck at it.

View of database

Changed the code, there aren't any Errors right now, but still it doesn't works.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Kropek
  • 31
  • 1
  • 1
  • 3

3 Answers3

4

I think you are following this tutorial, which is actually made by me.

The problem in your code is in the following line:

DocumentReference selectedDoc = docRef.collection("products").document(shoppingListId);

You cannot delete a product using only that line of code. Your code is not complete. To solve this, change that line with:

DocumentReference productIdRef = rootRef.collection("products").document(shoppingListId)
            .collection("shoppingListProducts").document(productId);
productIdRef.delete().addOnSuccessListener(aVoid -> Snackbar.make(shoppingListViewFragment, "Product deleted!", Snackbar.LENGTH_LONG).show());

If you want to delete a ShoppingList, first you need to delete everything that is beneath it. So in order to delete a particular shopping list, first you need to find all the documents beneath the shoppingListProducts collection, delete them and right after that you'll be able to delete the shoppingListId document.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • That's right Alex, I followed your tutorial, I have to say that it's great but I don't get this thing with references in Firebase :/ According to your response, maybe that was the issue because I tried to delete a document with some collections assigned to it. Am I supposed to delete all products that are added to this shoppingList first and then the empty document? I will try :) – Kropek Mar 16 '18 at 16:57
  • I managed to delete from view and from database currently selected shoppingListId. The following code solved half of my question: FirebaseFirestore docRef = FirebaseFirestore.getInstance(); DocumentReference selectedDoc = docRef .collection("shoppingLists") .document(userEmail) .collection("userShoppingLists") .document(shoppingListId); selectedDoc.delete(); But there are still products of this list remaining in databse and Im stuck here again, because I dont know how to delete all documents with productId from selected shoppingListId at once from selected collection. – Kropek Mar 16 '18 at 20:01
  • You say that "are still products of this list remaining in databse ". The products within a shopping list exist only beneath a shopping list. At least this is what I did in that tutorial. If you have saved the product in another place, then in same way you added them, you need to delete them. There is a write batch that can help. See also my answer from this [post](https://stackoverflow.com/questions/49125183/how-to-model-this-structure-to-handle-delete). Please, keep me posted. – Alex Mamo Mar 17 '18 at 06:05
  • Is there everything alright? Have you solved the issue? – Alex Mamo Mar 19 '18 at 09:52
2
FirebaseFirestore docRef = FirebaseFirestore.getInstance();
docRef.collection("products").document(shoppingListId)
     .delete()
     .addOnSuccessListener(new OnSuccessListener<Void>() {
     @Override
     public void onSuccess(Void aVoid) {
           
           // You can write what you want after the deleting process.
     })
     .addOnFailureListener(new OnFailureListener() {
     @Override
     public void onFailure(@NonNull Exception e) {
                                       
     }
     });
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/27780780) – double-beep Dec 20 '20 at 12:57
0
DocumentReference documentReference = firestore.collection("products").document(productid);
                                            documentReference.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {
                                                    if (task.isSuccessful()) {
                                                        Toast.makeText(Payment_Successful.this, "ok", Toast.LENGTH_SHORT).show();
                                                    } else {
                                                        Toast.makeText(Payment_Successful.this, "Not", Toast.LENGTH_SHORT).show();
                                                    }
                                                }
Amit Maity
  • 15
  • 4
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 01 '21 at 15:34