Cote Mounyo
Cote Mounyo

Reputation: 13965

delete all sharedPreferences for my app when user signs out

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?

Upvotes: 16

Views: 26623

Answers (8)

Piotr Zawadzki
Piotr Zawadzki

Reputation: 1678

You can clear all Shared Preferences with e.g.

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

Upvotes: 0

Dankyi Anno Kwaku
Dankyi Anno Kwaku

Reputation: 1293

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

Upvotes: 1

Khalid LACHHAB
Khalid LACHHAB

Reputation: 11

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

Upvotes: 1

Pratik Butani
Pratik Butani

Reputation: 62419

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

Upvotes: 3

Dhaval Patel
Dhaval Patel

Reputation: 694

Use the code below to delete a preference key:

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

Upvotes: 3

kalyan pvs
kalyan pvs

Reputation: 14590

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

Upvotes: 23

Exceptional
Exceptional

Reputation: 3004

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

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

Upvotes: 11

kwishnu
kwishnu

Reputation: 1851

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?

Upvotes: 0

Related Questions