ggcarmi
ggcarmi

Reputation: 468

delete multiple documents in Firebase Firestore (Android)

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?

Upvotes: 1

Views: 2694

Answers (1)

jBegera
jBegera

Reputation: 415

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

Upvotes: 2

Related Questions