2
2

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 1 year has passed since last update.

Firestoreでサブコレクション・サブドキュメントを削除したい

Last updated at Posted at 2021-12-29

どんな人向けか

Firestoreでデータベースを管理している場合に、サブコレクション・サブドキュメントも一緒に削除したい人

概要

公式にあるようにFirestoreでは親ドキュメントを削除しても子コレクション・子ドキュメントは削除されない
https://firebase.google.com/docs/firestore/manage-data/delete-data?hl=ja

だめな例

FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;

static deleteAccount() async {
  await firebaseFirestore.collection('users').doc('mail').delete();
}

いい例

FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;

static deleteAccount() async {
  await firebaseFirestore
      .collection('users')
      .doc('mail')
      .collection('name')
      .get()
      .asStream()
      .forEach((element) {
    for (var element in element.docs) {
      element.reference.delete();
    }
  });
}

上記のように再帰的に削除しなければならない。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?