Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear storage cache of an Android app on update

Tags:

android

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?

like image 689
MattTheHack Avatar asked Oct 17 '25 18:10

MattTheHack


2 Answers

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

like image 62
duggu Avatar answered Oct 20 '25 08:10

duggu


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

like image 35
HyLian Avatar answered Oct 20 '25 07:10

HyLian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!