Ahsan Farooq
Ahsan Farooq

Reputation: 41

How to delete list item inside document in firebase firestore in android?

I have a list stored inside a document of Firestore. I want to delete a specific item from the list how to delete this item!

I have visited many websites but did not find a suitable solution. Anyone can give me a reference about this problem.

Upvotes: 1

Views: 676

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

If you want to delete an element from your BpGR...sB6r array, you have to pass to the arrayRemove() method the entire data of the element you want to delete. It will not work if you just pass partially data.

In your case, to remove the first object entirely, for example, it means that you'll have to create a HashMap that will contain all data of the element and pass that to the update() method. For Android, this will look like this:

HashMap<String, Object> elementToDelete = new HashMap<>();
elementToDelete.put("audioUrl", null);
elementToDelete.put("drawingUrl", null);
elementToDelete.put("fromFirebase", true);
elementToDelete.put("imageUrl", null);
elementToDelete.put("text", "");
elementToDelete.put("videoUrl", "https://........"); //Add the exact URL
docRef.update("diaryUploadModelList", FieldValue.arrayRemove(elementToDelete));

If you don't know the entire data of the document, it won't work.

Another option might be to read the document, modify the diaryUploadModelList in memory, and write the modified diaryUploadModelList back to the document.

Upvotes: 1

Related Questions