How can I clear the Android app cache?

Posted by Srao on Stack Overflow See other posts from Stack Overflow or by Srao
Published on 2011-12-12T12:40:10Z Indexed on 2012/12/12 23:04 UTC
Read the original article Hit count: 185

Filed under:

I am writing a app which can programatically clear application cache of all the third party apps installed on the device. Following is the code snippet for Android 2.2

public static void trimCache(Context myAppctx) {

    Context context = myAppctx.createPackageContext("com.thirdparty.game", 
            Context.CONTEXT_INCLUDE_CO|Context.CONTEXT_IGNORE_SECURITY);

    File cachDir = context.getCacheDir();        
    Log.v("Trim", "dir " + cachDir.getPath());

    if (cachDir!= null && cachDir.isDirectory()) {

        Log.v("Trim", "can read " + cachDir.canRead());
        String[] fileNames = cachDir.list();
        //Iterate for the fileName and delete
    }
}

My manifest has following permissions:

android.permission.CLEAR_APP_CACHE
android.permission.DELETE_CACHE_FILES

Now the problem is that the name of the cache directory is printed but the list of files cachDir.list() always returns null. I am not able to delete the cache directory since the file list is always null.

Is there any other way to clear the application cache?

© Stack Overflow or respective owner

Related posts about android