9

i want to clear data cache programmatic of my application that is increasing, right now i am clearing from Settings->Applications->Manage Application->My Application->Clear Cache.

but i want to do it by programmatic, please help me.

Rais Alam
  • 6,892
  • 12
  • 51
  • 84
aamitgupta
  • 121
  • 1
  • 1
  • 6

2 Answers2

9

When you calling this class, it`ll calculate all installed application cache files and then simply delete it from your phone, which are not affected to database or your personal data. it will boost your phone and make it faster, cache file is removed

public class MyApplicationClass extends Application {

    private static MyApplicationClass instance;

    @Override
    public void onCreate() 
    {
        super.onCreate();
        instance = this;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    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();
    }
}
Iain
  • 6,262
  • 2
  • 29
  • 49
Vaishali Sutariya
  • 5,103
  • 29
  • 32
  • if you want a button to do this you need to call MyApplicationClass.getInstance(). clearApplicationData() from within an onClickListener – Vaishali Sutariya Jul 07 '14 at 11:26
  • Can you add an explanation of what this code does, rather than just a code drop. – Iain Jul 07 '14 at 11:41
  • when you calling this class , it`ll calculate all installed application cache files and then simply delete it from your phone which are not affected to Database or your personal Data. it will boost your phone and make it faster, cache file is removed – Vaishali Sutariya Jul 07 '14 at 11:48
5

Just a guess:

Delete these files by knowing their absolute path to cache directory - getCacheDir() - http://d.android.com/reference/android/content/Context.html#getCacheDir().

nestor10
  • 350
  • 3
  • 12