1

I build social media android app (like Instagram).

I am trying to delete multiple documents in a collection in Fire store, but i didn't find information about how its done in android.

I made reference to the collection, and a Query with that reference (with a condition) and I assume I need to use batch but I cant find how.

my Database structure is:

feed/
    user_id/
        posts/
            post_id(its Document - the post itself)

and the code for finding the relevent posts to delete is:

db.collection(DBConst.DB_FEED).document(FirebaseAuth.getInstance().getUid())
.collection(DBConst.DB_POSTS).whereEqualTo(DBConst.DB_UID, currUserId);

how should it done?

ggcarmi
  • 458
  • 4
  • 17
  • we cant help if you dont provide the code that you have tried, and point out where are you having troubles – Gastón Saillén May 21 '18 at 20:55
  • Unless you show what you've tried already, it is going to be hard to help better than the documentation already does: https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes – Frank van Puffelen May 21 '18 at 21:02
  • i edit the question – ggcarmi May 22 '18 at 07:22
  • Possible duplicate of [How to model this structure to handle delete](https://stackoverflow.com/questions/49125183/how-to-model-this-structure-to-handle-delete) – Alex Mamo May 22 '18 at 07:43

1 Answers1

2

Unfortunately, there is no straight forward solution... however you can use batch delete.

val batch = db.batch()
db.collection(...).whereEqualTo(...).get().result.forEach { 
    batch.delete(it.reference) 
}
batch.commit()
jBegera
  • 405
  • 2
  • 12