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?

3
  • stackoverflow.com/questions/3687315/deleting-shared-preferences Commented Aug 21, 2013 at 4: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(); Commented Aug 21, 2013 at 4:22
  • What's the problem with subsequently clearing every preference file, i.e. in a loop? Commented Aug 21, 2013 at 4:38

8 Answers 8

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..

Sign up to request clarification or add additional context in comments.

3 Comments

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/"
@YoracoGonzales context.getFilesDir().getPath() gives me /data/user/0/{applicationId}/files which is not equal to /data/data/
context.filesDir.parentFile.absolutePath + File.separator + "shared_prefs" did work. But who knows maybe it will not work on Samsung ¯_(ツ)_/¯
11

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

getApplicationContext().getSharedPreferences("CREDENTIALS", 0).edit().clear().commit();

1 Comment

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();
3

Use the code below to delete a preference key:

prefs.edit().remove("YOURTAG1").commit();
prefs.edit().remove("YOURTAG2").commit();

Comments

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();

2 Comments

what is A_PREFS_FILE
The question asked for A_PREFS_FILE so, You can write your preference name.
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();
}

Comments

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());

Comments

1

You can clear all Shared Preferences with e.g.

val sharedPreferenceFile = File("${application.dataDir.path}/shared_prefs/")
sharedPreferenceFile.listFiles()?.forEach(File::delete)

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.