4

When an app is uninstalled, is the data in its SharedPreferences database deleted?

If not, without a device being rooted, is there any way to clear that data? (Or at least view it?)

8
  • SharedPreferences is located in the data directory of the app which gets deleted during uninstallation. Do you have a case where it was not deleted by the system?
    – Firelord
    Jun 12, 2019 at 20:36
  • @Firelord I was reading up on Stack Overflow on how to make an app with a "trial period". One of the answers recommended writing data to SharedPreferences, because they claimed it can remain when an app is uninstalled. That doesn't match what I remembered, so I thought it best to ask. Jun 12, 2019 at 20:41
  • SharedPreferences are backed by Google if the app participates in the backup. It would be best if you clarify whether "they" assumed your app would be backed up.
    – Firelord
    Jun 13, 2019 at 5:58
  • @Firelord I don't back up anything to Google (well, not intentionally), so I'm looking up what you're referring to. I see a "Back up my data" option deep within Android Settings... is that it? Jun 13, 2019 at 6:34
  • 1
    If auto backup is the issue, then you can clear the app data after reinstalling to delete all the shared preferences.
    – Andrew T.
    Jun 14, 2019 at 10:03

2 Answers 2

6

SharedPreferences are actually saved in a file in the app private directory: /data/data/package_name/shared_prefs/somefilename.xml

When an app is installed this directory and file are automatically created. When an app is uninstalled this directory is deleted along with its parent /data/data/package_name UNLESS the app is uninstalled in order to update it. From command line this is done using the flag -k or -r that is

adb uninstall -k package_name - Keep the data and cache direct­ories around after removal

adb install -r package_name - Replace existing application (effectively reinstall an existing app, keeping its data)

5

SharedPreferences database always deleted when app is uninstalled. But when in the AndroidManifest.xml allowBackup is set true then data is not cleared even if application is uninstalled and re-installed.

android:allowBackup="true"

So, always keep allowBackup to false when you don't want the data to be stored.

2
  • Thank you. What is the best way to keep allowBackup as false? Jun 14, 2019 at 6:35
  • 3
    @user298642 The allowBackup flag usually only affects the adb backup. Do you have a source that backs your answer?
    – Robert
    Jun 14, 2019 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .