Skip to main content

Clear Cache in Android programmatically

https://medium.com/@droidbyme/clear-cache-in-android-programmatically-26a1405a470b



If you are looking for delete cache of your own application then simply delete your cache directory and that’s all.
public static void deleteCache(Context context) {
 try {
 File dir = context.getCacheDir();
 deleteDir(dir);
 } catch (Exception e) {}
}
public 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();
 } else if(dir!= null && dir.isFile()) {
 return dir.delete();
 } else {
 return false;
 }
} 
By clicking on clear cache button in your code, call deleteCache(this) method.

Comments