1

I am using the below code for clear data in android application, but its not clearing all the data. When I see the app info after using this code, its show 30kb in data. I also see lots of tutorials in stackoverflow, but I am not able to clear all the data from my application.

 public void clearApplicationData() {
      File cache = getCacheDir();
      File appDir = new File(cache.getParent());
      if(appDir.exists()){
         //Log.i("TAG", "check the delete data==" +" DELETED");
       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++) {
             Log.i("TAG", "check the delete data==" + children[i] +" DELETED count="+ i+" length="+children.length );
             boolean success = deleteDir(new File(dir, children[i]));
             if (!success) {
                 return false;
             }
         }
     }
   return dir.delete();

}
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
jiten
  • 180
  • 1
  • 2
  • 12
  • look at this http://android-sample-code.blogspot.com/2012/01/how-to-clear-cache-data-in-android.html – Farhan Shah Apr 01 '14 at 09:56
  • possible duplicate of [How to clear cache Android](http://stackoverflow.com/questions/6898090/how-to-clear-cache-android) – Szymon Apr 01 '14 at 10:01
  • i also try both the tutorials, but its not clearing all the data from the application. because when i open the application info its show 36.00 kb data. – jiten Apr 01 '14 at 10:11
  • and also getting the notification from the gcm. – jiten Apr 01 '14 at 10:11

1 Answers1

0

private void clearPreferences() {

try {

    // clearing app data
    Runtime runtime = Runtime.getRuntime();
    runtime.exec("pm clear YOUR_APP_PACKAGE_GOES HERE");

} catch (Exception e) {
    e.printStackTrace();
}

}

can certainly solve the issue.