How to Remove Shared Preference While Application Uninstall in Android

how to remove shared preference while application uninstall in android

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

How to delete shared preferences data from App in Android

To remove specific values: SharedPreferences.Editor.remove() followed by a commit()

To remove them all SharedPreferences.Editor.clear() followed by a commit()

If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.

Delete shared preferences after app uninstall without using android:allowBackup=false

You can use BroadcastReceiver for it

Add this to Manifest

<receiver android:name=".DeleteReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>

And BroadcastReceiver class

public class DeleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//remove preferences
SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
settings.edit().clear().commit();
}
}

UPDATE: after some research I found that you cannot receive something when your app is deleted.

The package that is being removed does not receive this Intent.

The only solution is to use second application to get it. Sorry for disappointing.

So in your case android:allowBackup=“false” is the only way to delete settings.

SharedPreferences not being removed on user uninstalling application

Since Nexus 6P is running Android M, I think Automatic Backups is the issue.

I think You can use allowBackup to stop that.

Check this answer: https://stackoverflow.com/a/32010582/336312

How to remove SharedPreferences when I remove app on Android 6

How to clean my SharedPreferences?

What are you experiencing is the default behavior of allowBackup, whch is true by default. From the documentation:

Whether to allow the application to participate in the backup and
restore infrastructure. If this attribute is set to false, no backup
or restore of the application will ever be performed, even by a
full-system backup that would otherwise cause all application data to
be saved via adb. The default value of this attribute is true.

if you want to disable it add allowBackup="false" to the Application tag on your AndroidManifest. This way the SharedPreferences won't be restored

Uninstalling of the app doesn't erase the value of SharedPreferences

It is the problem with some of the devices.Try deleting the data files manually when user quits the app.

File sharedPreferenceFile = new File("/data/data/"+ getPackageName()+ "/shared_prefs/");
File[] listFiles = sharedPreferenceFile.listFiles();
for (File file : listFiles) {
file.delete();
}

Also make sure you havent turned on the allowBackup as true because from android-23 by default backup stores app's data including preferences to cloud.Later when you uninstall then reinstall newer version you will to use restored preferences.

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

SharedPreferences are not being cleared when I uninstall

This is a new marshmallow feature.

Add android:allowBackup="false" tag inside your <application> object in your app manifest to disable this behaviour.

If android:allowBackup tag clashes with any other library you are using, you should add tools:replace="android:allowBackup" also.

Android remembers shared preferences after complete app uninstall

On (re)install, your app may be restoring files from Google auto-backup (via Google Drive). To disable this feature, you can explicitly set it to false in the manifest:

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

If you'd like more granular control over what is backed up/restored and what is not, you can include or exclude specific files from the backups.

See auto backup documentation:
https://developer.android.com/guide/topics/data/autobackup#EnablingAutoBackup

If you don't want to disable auto backups, but want to reinstall with a "clean slate" (for testing purposes), you can do one of the following:

  • Clear app data after reinstall. This will wipe out files that were restored automatically on install
  • Clear app data prior to uninstall, then force a new backup (so old one gets reset) by using this command: adb shell bmgr backupnow <PACKAGE>

See how to test backups documentation:
https://developer.android.com/guide/topics/data/testingbackup#TestingBackup



Related Topics



Leave a reply



Submit