How to delete shared preferences data from App in Android
Asked Answered
P

29

598

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.

Parnas answered 10/9, 2010 at 18:34 Comment(0)
S
994

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.

Susumu answered 10/9, 2010 at 18:37 Comment(7)
context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit(); //remove all your prefs :)Jolynnjon
@yoshi there is definitely a remove() as well as a clear(). You use remove() to remove specific preferences, you use clear() to remove them all. The original question wasn't clear if they needed to remove them all or not.Susumu
Clearing the preferences file does not appear to actually delete it. In my own tests by watching the "Data" usage listed in the App Info panel, creating a new SharedPreference file adds 4KB to this value but using editor.clear().commit() does not reduce the number.Salman
@Jolynnjon Why don't you put your comment as a separate answer?Hooke
@Hooke wow this comment has more than hundred votes! I added the answer below with a slightly improvement ;-)Jolynnjon
@Jolynnjon better using .apply() instead of .commit()Geehan
In kotlin that is sharedPreferences.edit().clear().apply()Recidivism
P
215

My solution:

SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("text").commit();
Phosphorism answered 7/12, 2011 at 5:39 Comment(5)
Does that remove the variable TEXT only?Faceharden
@SiKni8 Yes, that removes only the key/value pair with key "text".Calamitous
I was able to get it to work because it's been a while but thank you for the response :)Faceharden
You saved my absolute sanity. :P THANK YOU. I was trying to clear the prefs for one element of an object at a time, and this works beautifully.Jeaz
Consider using apply() instead of commit() to do the task in a background thread.Blearyeyed
I
156

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();
Islander answered 29/3, 2013 at 7:56 Comment(3)
What will be the KeyName?Amative
How to delete the entire preference file, not just the preferences inside the file?Decadence
Use apply() instead of commit() to do the task in a background thread. commit() blocks and write data to storage instantly. apply() is asynchronous.Stylus
P
78

If it's not necessary to be removed every time, you can remove it manually from:

Settings -> Applications -> Manage applications -> (choose your app) -> Clear data or Uninstall

Newer versions of Android:

Settings -> Applications -> (choose your app) -> Storage -> Clear data and Clear cache

Parrish answered 18/1, 2012 at 20:28 Comment(5)
can we do this things programmatic.Jolt
@Jolt The other answer does it programatically.Jiujitsu
@Jolt - Some of us needed this answer. I was unsure how to remove SharedPrefs on an Android Emulator so that I could continue testing my code.Dita
Not there in android studio 2.2.2 10/18/2016.Crisper
Thanks, was just looking for deleting this way.Cleavage
J
28

Deleting Android Shared Preferences in one line :-)

context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit();

Or apply for non-blocking asynchronous operation:

this.getSharedPreferences("YOUR_PREFS", 0).edit().clear().apply();
Jolynnjon answered 27/8, 2015 at 8:24 Comment(1)
Best solution for me. There are few cases when synchronous is neccesary for sharedpreferences content.Geehan
D
25

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

Deluna answered 15/3, 2016 at 10:37 Comment(1)
exactly what I want. Thanks it clears all default preferencesRoughcast
M
17

As of API 24 (Nougat) you can just do:

context.deleteSharedPreferences("YOUR_PREFS");

However, there is no backward compatibility, so if you're supporting anything less than 24, stick with:

context.getSharedPreferences("YOUR_PREFS", Context.MODE_PRIVATE).edit().clear().apply(); 
Moss answered 10/9, 2017 at 22:17 Comment(0)
E
15

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();
}
Erland answered 14/3, 2017 at 14:41 Comment(0)
A
11

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.

Animus answered 10/3, 2013 at 6:3 Comment(2)
+1 for the useful tip on run-as. You'd think I would have found this after 3 yrs...Hubblebubble
This isn't the Sharedpreferences, it's the sqlite databaseAlfredoalfresco
W
11
Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
Wileen answered 7/7, 2014 at 11:28 Comment(0)
C
11

Clear them all:

PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply()
Crazyweed answered 1/3, 2017 at 12:46 Comment(0)
M
10

You can also just manually uninstall your app using your device. Then when you re-install your app, shared preferences have been reset.

Mythomania answered 19/9, 2014 at 16:32 Comment(2)
Thanks - i did not want to programmatically remove sharedPreferences so this worked for me.Maloney
On my device I have the option to "clear data" on the uninstall option, which worked for me and removed the need to reinstall my app.Jimenez
S
9

For Kotlin users it is fairly easy:

val sharedPref = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
 sharedPref.edit().clear().apply()
Sievert answered 10/8, 2021 at 17:12 Comment(0)
F
7

Try this code:

SharedPreferences sharedPreferences = getSharedPreferences("fake", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.clear().commit();
Frau answered 30/6, 2016 at 20:25 Comment(0)
A
7

If it is for your testing. You can use adb commands.

adb shell pm clear <package name>
Adachi answered 6/4, 2018 at 14:5 Comment(0)
S
6

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.

enter image description here

Steradian answered 11/8, 2020 at 22:30 Comment(0)
B
5

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

https://github.com/farruhha/SimplePrefs

Bunder answered 16/8, 2018 at 23:47 Comment(0)
B
5
String prefTag = "someTag";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
prefs.edit().remove(prefTag).commit();

This will delete the saved shared preferences with the name "someTag".

Barbaraanne answered 8/3, 2019 at 8:13 Comment(0)
L
4

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();
Lightless answered 16/12, 2016 at 12:25 Comment(0)
P
3
  • To remove a particular value,

SharedPreferences.Editor remove(String key) followed by a commit() or a apply()

  • To remove all the values,

    SharedPreferences.Editor clear() followed by a commit() or a apply()

Prenotion answered 27/12, 2017 at 17:28 Comment(0)
E
3

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

Ensign answered 3/12, 2019 at 10:6 Comment(0)
C
3

One line of code in kotlin:

getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit().clear().apply()
Chukker answered 17/2, 2021 at 18:28 Comment(0)
R
2

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();
    }
}
Reich answered 7/2, 2019 at 11:26 Comment(0)
M
1
new File(context.getFilesDir(), fileName).delete();

I can delete file in shared preferences with it

Maynardmayne answered 8/1, 2019 at 14:2 Comment(1)
All above solutions are wrong, only delete file is right one to delete shared preferencesKraut
M
0

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()
Mozambique answered 26/3, 2022 at 12:2 Comment(0)
F
0

Kotlin :

  var prefs2: SharedPreferences? = context!!.getSharedPreferences("loginFB", 0)
  prefs2!!.edit().remove("email").commit()
Fitzgerald answered 26/7, 2022 at 11:10 Comment(0)
K
-2

You can use preferences.edit().remove("key").commit() to delete saved values from shared preferences.

Koffman answered 1/7, 2015 at 7:38 Comment(0)
K
-2

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()
        }
Kingery answered 7/8, 2021 at 21:35 Comment(0)
J
-5

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.

Jael answered 21/12, 2010 at 12:48 Comment(4)
So whoever down voted this solution should maybe also explain why: You need a rooted phone to access /data/data directly!Bechance
You need to Remove the SharedPreferences form the Application and not the ADB Shell.It is done using: SharedPreferences.edit().clear().commit();Subminiature
Asked never said they wanted a no root solution. There is a technical error here though; because this method requires root, you have to run su after adb shell to open up a privileged shell.Recording
Why was this downvoted so much? This is a useful answer and the one I was looking for.Bisectrix

© 2022 - 2024 — McMap. All rights reserved.