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.
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.
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 remove the key-value pairs from preference, you can easily do the following
getActivity().getSharedPreference().edit().remove("key").apply();
I have also developed a library for easy manipulation of shared preferences. You may find the following link
String prefTag = "someTag";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
prefs.edit().remove(prefTag).commit();
This will delete the saved shared preferences with the name "someTag".
For Kotlin users it is fairly easy:
val sharedPref = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
sharedPref.edit().clear().apply()
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
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();
}
}
One line of code in kotlin:
getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit().clear().apply()
new File(context.getFilesDir(), fileName).delete();
I can delete file in shared preferences with it
My Answer:
In Java:
SharedPreferences myPrefs = context.getSharedPreferences("My_Pref", Context.MODE_PRIVATE);
myPrefs.edit().remove("my_key").apply();
In Kotlin:
val myPrefs = context.getSharedPreferences("My_Pref", Context.MODE_PRIVATE)
myPrefs.edit().remove("my_key").apply()
Kotlin :
var prefs2: SharedPreferences? = context!!.getSharedPreferences("loginFB", 0)
prefs2!!.edit().remove("email").commit()
This is my Kotlin method:
public fun clearAllSharedPrefs() {
val sharedPreferences: SharedPreferences = MainApplication.applicationContext()
.getSharedPreferences("MY_CUSTOME_KEY", Context.MODE_PRIVATE)
sharedPreferences.edit().clear()
sharedPreferences.edit().apply()
}
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.