How do I delete SharedPreferences data for my application?
I'm creating an application that uses a lot of web services to sync data. For testing purposes, I need to wipe out some SharedPreferences values when I restart the app.
To remove specific values: SharedPreferences.Editor.remove() followed by a commit()
To remove them all SharedPreferences.Editor.clear()
followed by a commit()
If you don't care about the return value and you're using this from your application's main thread, consider using apply()
instead.
Removing all preferences:
SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
settings.edit().clear().commit();
Removing single preference:
SharedPreferences settings = context.getSharedPreferences("PreferencesName", Context.MODE_PRIVATE);
settings.edit().remove("KeyName").commit();
Seems that all solution is not completely working or out-dead
to clear all SharedPreferences in an Activity
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().clear().apply();
Call this from the Main Activity after onCreate
note* i used .apply()
instead of .commit()
, you are free to choose commit();
You can use the adb shell to do this even without a rooted phone. The only catch is that the app must be debuggable.
run-as <your package name> <command>
For example:
run-as com.asdf.blah rm /data/data/com.asdf.blah/databases/myDB.db
Alternatively, you can just do the above but without the command which will direct you to the app package root and allow you to execute more commands in the app's context.
In the class definitions:
private static final String PREFERENCES = "shared_prefs";
private static final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(PREFERENCES, MODE_PRIVATE);
Inside the class:
public static void deleteAllSharedPrefs(){
sharedPreferences.edit().clear().commit();
}
You can always do it programmatically as suggested by the other answers over here. But for development purpose, I find this Plugin
very helpful as it speeds up my development significantly.
PLUGIN: ADB Idea
It provides you with features to Clear App Data and Revoke Permission from your Android Studio itself, just with click of a button.
To clear all SharedPreferences centrally from any class:
public static SharedPreferences.Editor getEditor(Context context) {
return getPreferences(context).edit();
}
And then from any class: (commit returns a Boolean where you can check whether your Preferences cleared or not)
Navigation.getEditor(this).clear().commit();
Or you can use apply; it returns void
Navigation.getEditor(this).clear().apply();
None of the answers work for me since I have many shared preferences keys.
Let's say you are running an Android Test instead of a unit test.
It is working for me loop and delete through all the shared_prefs files.
@BeforeClass will run before all the tests and ActivityTestRule
@BeforeClass
public static void setUp() {
Context context = InstrumentationRegistry.getTargetContext();
File root = context.getFilesDir().getParentFile();
String[] sharedPreferencesFileNames = new File(root, "shared_prefs").list();
for (String fileName : sharedPreferencesFileNames) {
context.getSharedPreferences(fileName.replace(".xml", ""), Context.MODE_PRIVATE).edit().clear().commit();
}
}
The Kotlin ktx way to clear all preferences:
val prefs: SharedPreferences = getSharedPreferences("prefsName", Context.MODE_PRIVATE)
prefs.edit(commit = true) {
clear()
}
Click here for all Shared preferences operations with examples
Just did this this morning. From a command prompt:
adb shell
cd /data/data/YOUR_PACKAGE_NAME/shared_prefs
rm * // to remove all shared preference files
rm YOUR_PREFS_NAME.xml // to remove a specific shared preference file
NOTE: This requires a rooted device such as the stock Android virtual devices, a Genymotion device, or an actual rooted handset/tablet, etc.