I am an android developer doing an update to my users of an Android app and my desire is to clear the app storage for the app on all users devices as the data packaged with the app has expired. Is it possible to do this?
Try below code:-
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
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();
}
manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
see below link for more info:-
Clear Application's Data Programmatically
You can query your files using String[] fileList()
method of your Context
object and then deleteFile(String)
them.
Watch out for Exceptions while deleting files in case you don't have permission to delete that item
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With