0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter、Firestoreで500件以上のbatch処理

Posted at

Flutter、Firestoreで500件以上のbatch処理

結果

Future deletePostsOfCurrentUser(User? currentUser) async {
    await FirebaseFirestore.instance
    .collection('posts')
    .where('uid',isEqualTo: currentUser!.uid)
    .get()
    .then((qshot) {
      WriteBatch batch = FirebaseFirestore.instance.batch();
      final docs = qshot.docs;
      int index = 0;
      docs.forEach((DocumentSnapshot doc) async {
        if ((index + 1) % 500 == 0) {
          // commit by 500 and initialize batch instance
          await batch.commit();
          batch = FirebaseFirestore.instance.batch();
        }
        batch.delete(doc.reference);
        index++;
      });
      // last commit
      return batch.commit();
    });
  }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?