14

I have an android app with a number of activities, and a number of specific preference files that are created. When a user sign out of my app, I want to delete absolutely all shared preferences. Understand that I cannot use context.getSharedPreferences("A_PREFS_FILE", 0).edit().clear().commit() as that would only clear one specific file. I want to clear all the preference files associated with my app. Is there a way to do that?

Cote Mounyo
  • 13,367
  • 21
  • 64
  • 85
  • http://stackoverflow.com/questions/3687315/deleting-shared-preferences – Raghunandan Aug 21 '13 at 04:17
  • `clear()` is a non-static method, and therefore `SharedPreferences.Editor.clear()` would not work. All the other examples there are doing exactly what I specifically said I don't want to do. Also, following would be a null pointer: `Editor defaultPrefsPut; defaultPrefsPut.clear(); defaultPrefsPut.commit();` – Cote Mounyo Aug 21 '13 at 04:22
  • What's the problem with subsequently clearing every preference file, i.e. in a loop? – MH. Aug 21 '13 at 04:38

7 Answers7

23

By this way you can delete all the files at ones.. by clear() it will erase the data file still exist..

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

Here it will returns the all the list of files then you can easily delete..

kalyan pvs
  • 14,761
  • 3
  • 40
  • 57
  • 6
    This is definitivly the right way to delete all shared preferences files. But consider to use `context.getFilesDir().getPath()` instead of hard coding the path `"/data/data/"` – Yoraco Gonzales Jan 07 '17 at 09:36
  • @YoracoGonzales `context.getFilesDir().getPath()` gives me `/data/user/0/{applicationId}/files` which is not equal to `/data/data/` – Jemshit Iskenderov Dec 12 '18 at 09:10
  • 3
    `context.filesDir.parentFile.absolutePath + File.separator + "shared_prefs"` did work. But who knows maybe it will not work on Samsung ¯\_(ツ)_/¯ – Jemshit Iskenderov Dec 12 '18 at 09:25
11

Simply put the following code, It works perfect for me.....

getApplicationContext().getSharedPreferences("CREDENTIALS", 0).edit().clear().commit();
Exceptional
  • 2,936
  • 1
  • 16
  • 24
  • Consider using apply() instead; commit writes its data to persistent storage immediately, whereas apply will handle it in the background. ```getApplicationContext().getSharedPreferences("PREF_NAME", 0).edit().clear().apply();``` – Félix Maroy Dec 02 '20 at 06:50
3

First you have to clear then next call commit Try it:

SharedPreference.Editor pref = context.getSharedPreferences("A_PREFS_FILE", 0).edit();
pref.clear();
pref.commit();
Dharman
  • 26,923
  • 21
  • 73
  • 125
Pratik Butani
  • 56,749
  • 54
  • 254
  • 407
3

Use the code below to delete a preference key:

prefs.edit().remove("YOURTAG1").commit();
prefs.edit().remove("YOURTAG2").commit();
WrightsCS
  • 50,205
  • 22
  • 134
  • 184
Dhaval Patel
  • 694
  • 4
  • 12
2

This works

public static void deleteAllDataInSharedPreference(Context context){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();
    editor.clear();
    editor.commit();
    context = null;
}

deleteAllDataInSharedPreference(getApplicationContext());
Dankyi Anno Kwaku
  • 1,139
  • 3
  • 13
  • 24
1

SharedPreferences directory path : Environment.getDataDirectory() + "/data/" + ActivateAccountActivity.this.getPackageName() + "/shared_prefs"

Use "for" loop to delete all the files, like so :

File sprefs_directory = new File(Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/shared_prefs");
File[] files = filesirectory.listFiles();
for(File file : files) {
  file.delete();
}
0

Why not have a SharedPreference that keeps track of all associated files; then, in onPause or onStop, parse that value to SharedPreferences.Editor.clear().commit() each of them...then delete that last one?

kwishnu
  • 1,592
  • 17
  • 16