121

I have an android application to save the login details such as user name and password via SharedPreferences thats works fine, but i need to remove all my used SharedPreferences while my application uninstall. How to do it?

SavePreferences("one ", "");
SavePreferences("two", "");
LoadPreferences();

 private void SavePreferences(String key, String value){
    sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }

 private void LoadPreferences(){
    sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE);
    String strSavedMem1 = sharedPreferences.getString("MEM1", "");
    String strSavedMem2 = sharedPreferences.getString("MEM2", "");   
   } 

I want to remove this SharedPreferences when my application uninstall.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
shivcena
  • 2,023
  • 8
  • 24
  • 50
  • 13
    This will be removed automatically by Android OS on application uninstall... – umair.ali Apr 08 '13 at 07:01
  • thanks for the worthful detail – shivcena Apr 08 '13 at 07:06
  • 1
    Not on my rooted Samsung Galaxy 1 (Froyo). Still remained in `/dbdata/database/my.package/shared_prefs`. Don't know if this is beacuse it's rooted or because it's Froyo. Haven't been able to test with newer devices – Nilzor Oct 29 '14 at 12:29

6 Answers6

358

The problem is not with preferences. It's drastically the backup manager! .. since android-23 by default backup as a task stores app's data including preferences to cloud. Later when you uninstall then install newer version you are probably going to use restored preferences. To avoid that, just add this to your manifest (or at least to debug manifest):

<application ...
        android:allowBackup="false">
...
</application>

Read this: http://developer.android.com/guide/topics/data/backup.html

You will also see that if you run Lint under Android > Lint > Security:

lint warning on backup

It's good to mention here that the process of backup is like a blackbox .. you don't know when it starts, and period between checks ... so better for developing to disable it.

==== Update ====

You may get Manifest merger issues after setting allowbackup to false. To fix that issue add:

tools:replace="android:allowBackup"

in the application element. Credit to @shahzain-ali

Alternatively you can clear cache before uninstalling app.

I hope that may help.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • 5
    Hey, I have noticed that in some devices the shared preferences is retained after unistall util the device is restarted. Is that connected to this? If so is it safe to set this flag to false on production apps? Will it affect the user? – Eliahu Horwitz Aug 05 '16 at 22:42
  • Its not recommended to set false on production .. user will lose all preferences during update .. Better on production to migrate data if it necessary .. in debug code and preferences may change many times where is better to turn backup off – Maher Abuthraa Aug 07 '16 at 08:55
  • 1
    Kudos @MaherAbuthraa .. I was facing this issue of certain shared preference data being retained after app uninstall and reinstall. Never knew that such a thing was being done. – SoulRayder Jan 17 '17 at 16:49
  • 1
    How can I make sure preferences are preserved during update but removed on manual uninstall? Surely this would be the expected behaviour. – Gannet Aug 22 '17 at 08:47
  • @Gannet Preferences would have been removed after uninstallation . but might be restored from cloud later .. It's ultimately different from what's happening during upgrading app. when App being updated, system keeps preferences.To achieve your goal, just set that command above and your app will never restore preferences after uninstalling app. – Maher Abuthraa Aug 25 '17 at 20:57
  • @MaherAbuthraa thanks for the answer. I'm still a little confused though because you mentioned above that it isn't recommended to do this on production because the user will lose all preferences during update. Here's what I understand so far: 1. Prefs will always be removed after uninstall, regardless of allowBackup. 2. Prefs may be restored from cloud on reinstall, if allowBackup is true (personally I don't think this is appropriate). 3. Updates... not sure. – Gannet Aug 27 '17 at 08:32
  • @Gannet. lets start from issue that devices powered with Android-23+ might restored preferences after reinstall (not upgrade). Generally its issue with developers and QA. Because preferences might be changing frequently and developers expect that preferences will be lost during reinstalling. Thats why its recommend to disable backup on debug only. that in case app doesn't have backup configuration. In production, app should have migration plan how to move from earlier version of Preferences to newer one which is not really good practices to do so with debug versions during developing. clear ? – Maher Abuthraa Aug 31 '17 at 08:19
  • Even though I kept `android:allowBackup="false"` and `tools:replace="android:allowBackup"` still getting my preferences, Any idea what's the issue? – Shailendra Madda Apr 16 '18 at 06:43
  • @ShylendraMadda Check if you have this one specific phone or generally. you can try this attrib "fullBackupContent". some manufactures add backup-apps that don't respect attrib "android:allowBackup" . I would say for these kind of scenarios: go aggressively and clear the cache on the first launch of app for that particular phone if it's that critical issue. – Maher Abuthraa Jul 26 '18 at 04:06
  • I swear I had done this before.... after testing on a new phone I went back and the code was turned to true.... very very weird. – Delark Feb 05 '21 at 17:17
21

SharedPreferences is always deleted along with the app uninstall.

When you uninstall any application all the changes the application have made in your internal memory are revoked, that means your SharedPreference files, Other data files, Database file, Application gets removed automatically by the Android OS.

EDITED: 29/04/15: for >= 21 API refer @Maher Abuthraa 's answer

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • 13
    Not the case on Nexus 6P, figuring this out right now. – AndyRoid Jan 27 '16 at 05:11
  • 1
    Yes, I also observed in Nexus 5 running with Android 6 is not deleting the sharedpreferences. I had to restart the device after uninstall and the sharedpreferences is gone now – Santhana Jan 28 '16 at 20:54
  • @AndyRoid: i don't think its happening. i have nexus 5 with 6.1.1 its never happning. still you can search in bug list https://code.google.com/p/android/issues/ if there is no bug related to this then add new bug so they resolve its as soon as possible. – Dhaval Parmar Jan 29 '16 at 04:07
  • 2
    Galaxy s7 here with the same problem. I Uninstall and after install the sharedpreferences are still set. Really annoying because I can't test fresh installs by just uninstalling it. – Dpedrinha Apr 12 '16 at 21:09
  • 2
    @Maher Abuthraa has a right answer this answer is wrong. – Юрій Мазуревич Apr 26 '16 at 08:25
  • Galaxy s6 edge. and has the same issue. does not remove the values in the preference file. Should try @Maher 's answer – k9yosh Jun 02 '16 at 06:42
  • technically this answer is correct, the preferences do get deleted, but on 21+ with backup enabled, the preferences are restored after install – Tyler Oct 05 '16 at 16:30
  • 2
    Samsung s8 s8+ device as well not delete shared preference, I recently test. Really disappointed with Samsung – Vrajesh Apr 30 '18 at 19:14
19

Its strange but I found the solution in following way:

  1. Add xmlns:tools="http://schemas.android.com/tools" in manifest tag of Manifest.xml file
  2. Add android:allowBackup="false" in application tag of Manifest.xml file
  3. Add tools:replace="android:allowBackup" in application tag of Manifest.xml file

Manifest.xml file should looks like this.

    <?xml version="1.0" encoding="utf-8"?><!--suppress ALL -->
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.package">

        // Other code

    <application
        android:name="com.package.Application"
        android:allowBackup="false"
        android:hardwareAccelerated="true"
        android:icon="@drawable/appicon"
        android:label="@string/application_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme"
        tools:replace="android:allowBackup">

        <activity
            android:name="com.package.SplashActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/application_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        // Other code

    </application>

    </manifest>

Its done.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • 2
    Your issue was one of your modules/AARs has android:allowBackup="true" .. That's why you need to set it up again with attribute tools:replace. – Maher Abuthraa Nov 08 '17 at 21:48
  • Even though I kept `android:allowBackup="false"` and `tools:replace="android:allowBackup"` still getting my preferences, Any idea what's the issue? – Shailendra Madda Apr 16 '18 at 06:43
  • I am also getting the same issue. After adding this code not able to clear prefrence – Pawan Soni May 12 '20 at 11:17
11

The problem is not with preferences.

use this code for fix it..........

<application
    android:allowBackup="true"
    android:fullBackupContent="false"></application>
Mohammad Faizan
  • 191
  • 1
  • 4
5

Setting allowBackup="false" opts an application out of both backup and restore.

 android:allowBackup="false"
piet.t
  • 11,718
  • 21
  • 43
  • 52
Mahendran Candy
  • 1,114
  • 17
  • 17
1

Shared Preferences do not always get deleted anymore as of marshmallow. Add this line in your manifest: "android:allowBackup="false""