Clear firebase persistence after logout

16,239

Solution 1

clearPersistence() was launched in v6.2.0.

via

https://github.com/firebase/firebase-js-sdk/issues/449

Solution 2

Unfortunately there's no supported way to clear the persistence cache. You could try to manually delete the SQLite database used by Firebase Database (should be under /databases/ in your app's data folder), but you'd need to do it before initializing Firebase Database, so you'd have to force the app to restart after signing somebody out or similar.

Regarding knowing if there's "data waiting to sync", assuming you're talking about writes, the only way is to attach CompletionListeners to every write operation you do, and wait for them to complete.

Both of these are things we'd like to improve in future versions of the Firebase Database SDK.

Solution 3

    if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
        ((ActivityManager) getContext().getSystemService(ACTIVITY_SERVICE))
                .clearApplicationUserData();
    }

source

Share:
16,239
wrozwad
Author by

wrozwad

Updated on August 02, 2022

Comments

  • wrozwad
    wrozwad almost 2 years
    1. Is there any way to clear data stored in Firebase Database persistence enabled via setPersistenceEnabled(true)?
    2. Or eventually prevent read access to previously stored users data on the device?
    3. And extra question - is there any way to check if any data is waiting to sync?

    In my app I want to clear db when user is logging out and he confirm that unsynchronized changes will be removed. This app will be used on tablet by many people, and there'll be many data stored per user, so I want to clear unnecessary data stored on the device to:

    • shrink storage usage
    • prevent unauthorized person from read users data

    It's a security flaw because any user, regardless of whether is signed in or not, has read access to all previously downloaded data! Fortunately we haven't write access there.

    I tried to clear db on my own, in both offline and online mode, but data still persist on the device:

    // first clear all data on my own
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    db.goOffline(); // even when I clear db in online mode data persist on mobile device
    db.getReference().setValue(null);
    
    // and then logout
    FirebaseAuth.getInstance().signOut();
    db.goOnline();
    

    eg data structure:

    {
      "someList": {
        "userId1": {
          "itemId11": {
            "name": "name 11"
          },
          "itemId12": {
            "name": "name 12"
          },
          ...
          "itemId1N": {
            "name": "name 1N"
          }
        }
        "userId2": {
          "itemId21": {
            "name": "name 21"
          },
          "itemId22": {
            "name": "name 22"
          },
          ...
          "itemId2N": {
            "name": "name 2N"
          }
        }
      }
    }
    

    my rules:

    {
      "rules": {
        ".read": "false",
        ".write": "false",
        "someList": {
          "$uid": {
            ".read": "$uid === auth.uid",
            ".write": "$uid === auth.uid"
          }
        }
      }
    }
    
  • wrozwad
    wrozwad over 7 years
    @Michael Legenbauer - So... is there any progress with it?
  • Greg Ennis
    Greg Ennis about 7 years
    @michael-lehenbauer Any new progress on this? Due to firebase limitations of losing pending changes when app is terminated while offline, I'm seeing the local cache get out of sync with the database with no apparent way to resolve it. A way to clear the cache would be a good fallback.
  • Abhilash Das
    Abhilash Das over 6 years
    you could use keepSync(true)
  • Sheepdogsheep
    Sheepdogsheep about 6 years
    Any idea how I would do something similar in iOS?
  • Simon B.
    Simon B. almost 5 years
    Can you use clearPersistence during logout though? I've tried in a couple of ways, but can't stop the app. The terminate from Android SDK is not yet in the JS SDK it seems. I need to stop the app in order to be allowed to perform clearPersistence.
  • wal
    wal over 4 years
    there are a few caveats that should be explained in this answer - check the docs: firebase.google.com/docs/reference/android/com/google/fireba‌​se/… After consideration the best advice i have read is not to use persistence if you need to clear the cache.
  • Apoleo
    Apoleo over 3 years
    the question was about realtime database not firestore
  • Dan Horton
    Dan Horton almost 3 years
    I also thought this would be a good approach, however the problem is even worse. After I delete the app entirely (macOS), do a full clean and rebuild, then reinstall, and the old persistence cache still remains! How on earth is this possible, where is it storing the cache, any ideas?
  • Brandon
    Brandon almost 3 years
    how do you go about disabling persistence?