Andrew
Andrew

Reputation: 21076

How to delete shared preferences data from App in Android

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.

Upvotes: 603

Views: 526562

Answers (29)

Ashish Jaiswal
Ashish Jaiswal

Reputation: 804

You can use-> preferences.edit().remove("key").commit(); to delete saved value from shared preferences.

Upvotes: -2

Kingsley Mitchell
Kingsley Mitchell

Reputation: 2589

Kotlin :

  var prefs2: SharedPreferences? = context!!.getSharedPreferences("loginFB", 0)
  prefs2!!.edit().remove("email").commit()

Upvotes: 0

Saurabh Dhage
Saurabh Dhage

Reputation: 1711

For Kotlin users it is fairly easy:

val sharedPref = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
 sharedPref.edit().clear().apply()

Upvotes: 9

Shashank Adhatrao
Shashank Adhatrao

Reputation: 67

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

Upvotes: 0

Mohsen Emami
Mohsen Emami

Reputation: 3142

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

Upvotes: -2

Biplob Das
Biplob Das

Reputation: 3094

One line of code in kotlin:

getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit().clear().apply()

Upvotes: 3

Vishist Varugeese
Vishist Varugeese

Reputation: 1580

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

Upvotes: 6

Farruh Habibullaev
Farruh Habibullaev

Reputation: 2392

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

Upvotes: 5

sivi
sivi

Reputation: 11114

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

Upvotes: 15

Sufiyan Ansari
Sufiyan Ansari

Reputation: 1946

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

Upvotes: 4

Shady Keshk
Shady Keshk

Reputation: 570

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

Upvotes: 25

Paritosh
Paritosh

Reputation: 2137

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

Upvotes: 3

Kiran k g
Kiran k g

Reputation: 996

String prefTag = "someTag";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
prefs.edit().remove(prefTag).commit();

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

Upvotes: 5

Allen
Allen

Reputation: 3207

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

Upvotes: 2

Dũng Phạm Tiến
Dũng Phạm Tiến

Reputation: 349

new File(context.getFilesDir(), fileName).delete();

I can delete file in shared preferences with it

Upvotes: 1

Dan Alboteanu
Dan Alboteanu

Reputation: 10212

Clear them all:

PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply()

Upvotes: 11

vaibhav vijay
vaibhav vijay

Reputation: 2175

My solution:

SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("text").commit();

Upvotes: 215

Prakash
Prakash

Reputation: 7996

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

adb shell pm clear <package name>

Upvotes: 7

Palak Jain
Palak Jain

Reputation: 683

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

Upvotes: 3

Nobu
Nobu

Reputation: 10415

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

Upvotes: 80

afathman
afathman

Reputation: 6133

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

Upvotes: 19

Mark B
Mark B

Reputation: 200411

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.

Upvotes: 996

Mubashshir
Mubashshir

Reputation: 171

Try this code:

SharedPreferences sharedPreferences = getSharedPreferences("fake", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.clear().commit();

Upvotes: 7

rubdottocom
rubdottocom

Reputation: 8298

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

Upvotes: 28

wizurd
wizurd

Reputation: 3729

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

Upvotes: 10

kdahlhaus
kdahlhaus

Reputation: 480

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.

Upvotes: -5

Vaishali Sutariya
Vaishali Sutariya

Reputation: 5121

Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();

Upvotes: 11

sz12345
sz12345

Reputation: 1589

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

Upvotes: 158

Johnson Wong
Johnson Wong

Reputation: 4483

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.

Upvotes: 11

Related Questions