Firestore how to clearPersistence()

I need to clear current user data completely once user logout. Therefore I use clearPersistence() function. It need to run terminate() before clearPersistence().

 this.afs.firestore.terminate().then(() => {
      this.afs.firestore.clearPersistence().then(() => {

After run below above to clear firestore local cache, I receive this error.

FirebaseError: The client has already been terminated.

Any advice how to re initialise firestore instance or any better way to achieve? Thanks.


This isn’t the intended purpose of clearPersistence(). I suggest reading the API documentation carefully:

Must be called while the firestore instance is not started (after the app is shutdown or when the app is first initialized). On startup, this method must be called before other methods (other than settings()). If the firestore instance is still running, the promise will be rejected with the error code of failed-precondition.

Note: clearPersistence() is primarily intended to help write reliable tests that use Cloud Firestore. It uses an efficient mechanism for dropping existing data but does not attempt to securely overwrite or otherwise make cached data unrecoverable. For applications that are sensitive to the disclosure of cached data in between user sessions, we strongly recommend not enabling persistence at all.

The second paragraph is telling you that you probably just shouldn’t have persistence enabled at all.

The first paragraph is telling you that using clearPersistence during your test can only be done immediately after the Firestore app instance is first initialized (and before any query), or after the app is fully shut down.