4

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?

MattTheHack
  • 1,354
  • 7
  • 28
  • 48

3 Answers3

2

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

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
1

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

HyLian
  • 4,999
  • 5
  • 33
  • 40
1

You have to delete them manually, since you know what files YOU saved it should be easy to remove them on first App start after the update.

Edit: Too late -.-

Marvin
  • 226
  • 1
  • 9