Kropek
Kropek

Reputation: 41

How to delete selected document ID from Firestore

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.

Upvotes: 3

Views: 11936

Answers (3)

Amit Maity
Amit Maity

Reputation: 25

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();
                                                    }
                                                }

Upvotes: 0

Onur Sumakoglu
Onur Sumakoglu

Reputation: 21

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) {
                                       
     }
     });

Upvotes: 2

Alex Mamo
Alex Mamo

Reputation: 1

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.

Upvotes: 4

Related Questions