How to delete shared preferences data from App in Android

前端 未结 24 2212
日久生厌
日久生厌 2020-11-22 14:37

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

相关标签:
24条回答
  • 2020-11-22 15:33

    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.

    0 讨论(0)
  • 2020-11-22 15:34

    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();
    
    0 讨论(0)
  • 2020-11-22 15:35

    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

    0 讨论(0)
  • 2020-11-22 15:37

    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();
    
    0 讨论(0)
  • 2020-11-22 15:37

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

    adb shell pm clear <package name>
    
    0 讨论(0)
  • 2020-11-22 15:38

    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.

    0 讨论(0)
提交回复
热议问题