1

Manually I can do

Settings -> Applications -> Manage applications -> (choose my app) -> Clear data

How do I do this programmatically?

I need to delete all data, including those in shared preferences (, and variables).

Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87
  • instead of hardcoding `/data/data/com.example.applicationpackagename/.` how do I specify it? since according to @ChrisStratton I cannot use `getExternalFilesDir(null)` – Cote Mounyo Dec 03 '13 at 22:15

2 Answers2

0

Everything in your application is stored here /data/data/com.example.applicationpackagename/.

Your application is run by a user which has R/W access on this directory.

You just need to remove every files in it.

For information : Android - How to delete a whole folder and content?

Community
  • 1
  • 1
  • Not quite true. You **do not** have write access to all the files within that directory - for example, the libs/ folder which would contain jni libraries. However I don't believe clearing application would normally remove that. Also, you should not be assuming what the package's private directory will be, but rather discovering what it is on the particular device using the appropriate APIs. – Chris Stratton Dec 03 '13 at 19:40
  • thanks for the inputs. But, is the answer to use: `if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { new File(dir, children[i]).delete(); } }` or is that incorrect according to @ChrisStratton ? – Cote Mounyo Dec 03 '13 at 19:58
  • Well, you'd have to gracefully handle failure or proactively skip the folders you aren't allowed to delete. – Chris Stratton Dec 03 '13 at 20:09
  • that's fine than. Thank you. Will test and up you. Thanks. – Cote Mounyo Dec 03 '13 at 21:21
  • The file that I am to pass in, is it the same as `getExternalFilesDir(null)`? If so then it's not removing my shared preferences. I need to remove them too in that one call. tagging @ChrisStratton – Cote Mounyo Dec 03 '13 at 21:53
  • No. That would be the External storage, while this is the private directory on the Internal storage. – Chris Stratton Dec 03 '13 at 21:58
-2

You could use following code in which "packagename" could be your application package name like com.example.app1

Intent intent = new Intent();
intent.setData(Uri.parse("package:" + packageName));
Mureinik
  • 297,002
  • 52
  • 306
  • 350