16

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,817
  • 23
  • 66
  • 87
  • 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

8 Answers8

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,486
  • 4
  • 41
  • 59
  • 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 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 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,994
  • 1
  • 18
  • 25
  • 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
  • 30,962
  • 25
  • 85
  • 135
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
3

Use the code below to delete a preference key:

prefs.edit().remove("YOURTAG1").commit();
prefs.edit().remove("YOURTAG2").commit();
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Dhaval Patel
  • 694
  • 4
  • 12
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();
}
1

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());
Junior
  • 1,007
  • 4
  • 16
  • 26
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,730
  • 19
  • 17
0

You can clear all Shared Preferences with e.g.

val sharedPreferenceFile = File("${application.dataDir.path}/shared_prefs/")
sharedPreferenceFile.listFiles()?.forEach(File::delete)
Piotr Zawadzki
  • 1,678
  • 1
  • 18
  • 24