Firestore の 初期化 リセット方法(コレクションの削除)


はじめに

Firestoreをいろいろいじっていると個別のコレクションの削除や全部初期化をしたい場合があります。その場合は、Firebase CLIを利用すると簡単に操作することができます。

手順

Install Command

sudo npm install -g firebase-tools

Login

firebase login

Check if the command works

$ firebase projects:list
✔ Preparing the list of your Firebase projects
No projects found.

Delete Collections and Docs in project-aaa

$ firebase firestore:delete --all-collections -y --project project-aaa
Deleting the following collections: cities, citiest0921, citiest0922, citiest0923, users
Deleted 3 docs (Infinity docs/s)
Deleted 5 docs (29 docs/s)
Deleted 6 docs (19 docs/s)
Deleted 11 docs (30 docs/s)

オプション一覧

> firebase firestore:delete --help
Usage: firebase firestore:delete [options] [path]

Delete data from Cloud Firestore.

Options:
  -r, --recursive    Recursive. Delete all documents and subcollections at and under the specified level. May not be passed along with --shallow.
  --shallow          Shallow. Delete only documents at the specified level and ignore documents in subcollections. This action can potentially orphan documents nested in subcollections. May not be passed
                     along with -r.
  --all-collections  Delete all. Deletes the entire Firestore database, including all collections and documents. Any other flags or arguments will be ignored.
  -y, --yes          No confirmation. Otherwise, a confirmation prompt will appear.
  -h, --help         output usage information

参考文献