Is there some way to programmatically clear the cache of an android app when upgrading to a new version or during an installation?
-
what kind of cache ? – Blackbelt Apr 16 '15 at 12:34
-
specifically web cache.. i'm running a webview in my app – Lakmal Caldera Apr 16 '15 at 12:35
-
webView.clearDisk(true); if you want to clear also the file on the disk – Blackbelt Apr 16 '15 at 12:36
-
Is it possible to do only once during first time installation or an upgrade? I don't want to do it every time when the app is running. Problem is when I upgrade i have html remnants from my previous version of the app. When i manually clear the cache from app info, those remnants are removed and everything looks fine.. i want to programmatically do this during an upgrade. Is it possible? – Lakmal Caldera Apr 16 '15 at 12:39
-
first time installation makes no sense. About the upgrade you can add write a version number on the sharedpreference and check against this number every time your app starts – Blackbelt Apr 16 '15 at 12:41
-
@Blackbelt Doesn't clearing the app data also remove any data saved in the sharedpreferences? – Endzeit Apr 16 '15 at 12:43
-
@Endzeit OP wants to clear the cache of the WebView he is using, no the app's data – Blackbelt Apr 16 '15 at 12:44
-
@Blackbelt I was refering to uncertain-eer answer, but you're right. – Endzeit Apr 16 '15 at 12:49
4 Answers
android have clearApplicationUserData()
in ActivityManager Class.
this method will have the same effect as explicit hit on clear app data in settings do.
here's the reference--
http://developer.android.com/reference/android/app/ActivityManager.html#clearApplicationUserData()
hope this helps cheers!

- 350
- 3
- 13
-
actually i want the data to be intact, just clear the cache and only once during an upgrade. – Lakmal Caldera Apr 16 '15 at 12:42
-
then i would say this is possible duplicate of this question. http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache – Jivraj S Shekhawat Apr 16 '15 at 12:49
Revering to this answer you can clear the app's cache the follwing way:
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
Simply call deleteCache() everytime your app is updated. To check whether you app is updated or not you can simply save the version string into a SharedPreference and compare the saved value with the actual one.
public static boolean isNewVersion(Context context) {
String newVersion = "", oldVersion = "";
PackageManager manager = context.getPackageManager();
PackageInfo info = null;
try {
info = manager.getPackageInfo(context.getPackageName(), 0);
newVersion = info.versionName;
} catch (PackageManager.NameNotFoundException e) {
// TODO what to do?
}
oldVersion = getVersionString();
// returns whether the new version string differs (is also false for first install)
return !(oldVersion.equals(DEFVAL_STRING) || oldVersion.equals(newVersion));
}
public static String getVersionString(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return preferences.getString(KEY_VERSION, DEFVAL_STRING);
}
Write receiver with action "PACKAGE_REPLACED" in manifest.
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
// write logic to delete cache directory
// Get cache dir as context.getCacheDir()
}
}

- 3,324
- 2
- 17
- 22
If you just want this for a webview, you can add cache busting to your urls loaded by the webview. Add the app version to the querystring and you'll get a fresh download after users upgrade:
"http://www.stackoverflow.com?v=" + BuildConfig.VERSION_NAME

- 4,479
- 1
- 36
- 50