Clear Cache in Android programmatically

Droid By Me
1 min readJan 3, 2018

--

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.

--

--

Droid By Me
Droid By Me

Written by Droid By Me

By profession and passion : Android Developer

Responses (1)