108

How to remove some key/value pair from SharedPreferences ? I have put and I to remove that from prefs.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Damir
  • 54,277
  • 94
  • 246
  • 365

6 Answers6

247
SharedPreferences mySPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = mySPrefs.edit();
editor.remove(key);
editor.apply();

Here editor is the sharedPreferences editor.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • 12
    A small addition to this answer. From the android documentation: "If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead." – Sreedevi J Nov 15 '13 at 04:46
  • 3
    @silwar , why have you reedited the apply() back to a commit() ? knowing that the documentation says to use apply() unless we want to work with the return value, and neither the question or the answer fits into that case – Salim Mahboubi Aug 17 '15 at 10:00
13

It is important to note that, unless you're planning on doing something with the return value of the commit() call, there is absolutely no reason for using the synchronous commit() call instead of the asynchronous apply() call.

Keep in mind that if you're calling this from the main/UI thread, the UI is blocked until the commit() has completed. This can take upwards of around 100ms as apposed to about 5ms for the apply. That may not seem like much, but if done continually throughout an application, it will certainly add up.

So, unless you're planning on doing something like this, hopefully on a separate thread:

editor.remove(String key); 
boolean success = editor.commit();
if (!success) { 
    // do something 
}

You should instead be doing this:

editor.remove(String key); 
editor.apply();
SBerg413
  • 14,515
  • 6
  • 62
  • 88
  • I've been looking all over for a good (real world) explanation of when to use commit vs apply. This drives the point across clearly. – XMAN Sep 08 '18 at 04:10
10

It's very simple:

private SharedPreferences sharedPreferences() {
    return PreferenceManager.getDefaultSharedPreferences(mContext);
}

public void clearSharedPreferences() {
    sharedPreferences()
            .edit()
            .remove(SOME_KEY_1)
            .remove(SOME_KEY_2)
            .remove(SOME_KEY_3)
            .apply();
}
9
SharedPreferences.Editor.remove(key) 
commit();
jvperrin
  • 3,368
  • 1
  • 23
  • 33
Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • 1
    If you don't use the return value from remove method, and you're using this from your application's main thread, consider using apply() instead. – kartik srivastava Aug 11 '19 at 08:57
3

Here is how I tacked this issue.

First I created an instance of SharedPreference as

SharedPreferences mobilePreference;

then I used this sharedPreference as

mobilePreference = this.getSharedPreferences("in.bhartisoftwares.amit.allamitappsthree", Context.MODE_PRIVATE);

Here "in.bhartisoftwares.amit.allamitappsthree" is my package name and I am using Context.MODE_PRIVATE, because I want to manipulate this shared preference only for this package name.

Then I am deleting the selected sharedPreference (key of my sharedPreference is mobileString) as follows:

mobilePreference.edit().remove("mobileString").commit();

See the code as full below:

SharedPreferences mobilePreference = this.getSharedPreferences("in.bhartisoftwares.amit.allamitappsthree", Context.MODE_PRIVATE);
    mobilePreference.edit().remove("mobileString").commit();
Amit Mhaske
  • 471
  • 5
  • 10
1

Information

Just check sharedpref class is extended to Map that's why there is remove method

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(String key);
editor.apply();

Here editor is the sharedPreferences editor.

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81