Reputation: 840
I am allowing the user to create multiple SharedPreferences
files, but I also would like the option for them to delete these files. I know I could use internal storage, but that is not my question.
My question is: "How can I delete in code or manually (not just clear) a SharedPreferences
file?"
Upvotes: 28
Views: 36918
Reputation: 9061
I found (in Android 12, years after the original question), that if I delete the shared prefs file shortly after some code edits a pref value, applying the new pref might not finish until after I delete the file, at which point Android will recreate the file with all the previous values intact.
This also happened when I followed the recommendation from @inazaruk to clear the prefs before deleting the file, because I used apply() there. Perhaps that wouldn't cause a problem if I used commit() instead, but I found that clearing the prefs wasn't needed in my case.
Upvotes: 0
Reputation: 4775
Java:
public static boolean deleteSharedPreferences(Context context, String name) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.deleteSharedPreferences(name);
} else {
context.getSharedPreferences(name, MODE_PRIVATE).edit().clear().apply();
File dir = new File(context.getApplicationInfo().dataDir, "shared_prefs");
return new File(dir, name + ".xml").delete();
}
}
Kotlin:
companion object {
fun deleteSharedPreferences(context: Context, name: String): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.deleteSharedPreferences(name)
} else {
context.getSharedPreferences(name, MODE_PRIVATE).edit().clear().apply()
val dir = File(context.applicationInfo.dataDir, "shared_prefs")
return File(dir, "$name.xml").delete()
}
}
}
Upvotes: 12
Reputation: 1121
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.deleteSharedPreferences(preferencesKey);
} else {
try {
org.apache.commons.io.FileUtils.cleanDirectory(new File(context.getCacheDir().getParent() + "/shared_prefs/"));
} catch (IOException e) {
Log.e(TAG, "Cannot delete files in shared pref directory", e);
}
}
Upvotes: 10
Reputation: 10245
Here is an easy method to clear all the SharedPreferences for a given context, usefull for unit-tests
public static void clearSharedPreferences(Context ctx){
File dir = new File(ctx.getFilesDir().getParent() + "/shared_prefs/");
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
// clear each preference file
ctx.getSharedPreferences(children[i].replace(".xml", ""), Context.MODE_PRIVATE).edit().clear().commit();
//delete the file
new File(dir, children[i]).delete();
}
}
Note that when you are using this for Android Unit testing and you are using sharedpreferences in your Application
class, this might cause a race condition and it might not work properly.
Upvotes: 32
Reputation: 9442
Its simple Genius!
Your default sd card preference.xml file path might be: /data/data/your package name/shared_prefs/your shared preference xml file.
like, /data/data/com.hirecraft.hirecraftmanager/shared_prefs/swipe_preferences.xml.
delete preference xml file:
File deletePrefFile = new File("/data/data/com.hirecraft.hirecraftmanager/shared_prefs/swipe_preferences.xml");
deletePrefFile.delete();
Or get file path in String like,
String filePath = getApplicationContext().getFilesDir().getParent()+"/shared_prefs/swipe_preferences.xml";
File deletePrefFile = new File(filePath );
deletePrefFile.delete();
Upvotes: 4
Reputation: 74790
If you get SharedPreferences
instance via Context.getSharedPreferences("X")
, then your file will be named X.xml
.
It will be located at /data/data/com.your.package.name/shared_prefs/X.xml
. You can just delete that file from the location. Also check /data/data/com.your.package.name/shared_prefs/X.bak
file, and if it exists, delete it too.
But be aware, that SharedPreferences
instance saves all data in memory. So you'll need to clear preferences first, commit changes and only then delete preferences backing file.
This should be enough to implement your design decision.
Upvotes: 36