19

I read through the Android documentation of the cache (see Data Storage Documentation) but I didn't got how I can clean the whole folder.

So how can I delete the cache-folder of my app? It's in this path:

/Android/data/de.stepforward/cache/

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
safari
  • 7,565
  • 18
  • 56
  • 82

5 Answers5

35

Put this code in onDestroy() to clear app cache:

void onDestroy() { super.onDestroy();

    try {
        trimCache(this);
       // Toast.makeText(this,"onDestroy " ,Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void trimCache(Context context) {
    try {
       File dir = context.getCacheDir();
       if (dir != null && dir.isDirectory()) {
          deleteDir(dir);
       }
    } catch (Exception e) {
       // TODO: handle exception
    }
 }

 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;
          }
       }
    }

    // The directory is now empty so delete it
    return dir.delete();
 }
katzenhut
  • 1,742
  • 18
  • 26
Chirag
  • 2,321
  • 24
  • 36
  • @crony_cd i m using this code after running this code i went to Android--->data-->cache-->my package name ---> and all files are still there ...... – user3233280 Dec 25 '14 at 09:18
  • Sidenote: If you are making files in the cache you want deleted on exit then you should use `File`'s `deleteOnExit()` method – Baggers Feb 07 '17 at 11:46
  • @user3233280 `Android/data/package name/cache` is `context.getExternalCacheDir()` – Doctor.Who. May 17 '18 at 09:16
  • Please note that onDestroy is not guaranteed to be called: https://developer.android.com/reference/android/app/Activity.html#onDestroy() – Mikhail Aug 26 '22 at 08:20
15

You can use the code referenced here:

https://stackoverflow.com/a/7600257/327011

File cacheDir = context.getCacheDir();

File[] files = cacheDir.listFiles();

if (files != null) {
    for (File file : files)
       file.delete();
}
Community
  • 1
  • 1
neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • 1
    this will not delete all files in the system -- a Directory must be empty before it can be deleted... – Dan Dec 02 '11 at 19:19
8

Kotlin:

You can make use of File.deleteRecursively() from the standard library to remove all sub directories as well

To delete the whole cache directory of the app:

context.cacheDir.deleteRecursively()

To delete a specific directory in the cache including its sub directories

File(context.cacheDir, "child directory name").deleteRecursively()

Thanks for the suggestion @elyeante

Mohamed Khaled
  • 139
  • 1
  • 7
4

Rather than rolling your own utility methods, you may want to consider using the apache commons FileUtils library. It contains a lot of useful File manipulation methods and makes operations like this very trivial.

Here are the JavaDocs

And here is an example:

try {
    FileUtils.deleteDirectory(context.getCacheDir());
} catch (IOException e) {
    Log.e(LOGTAG,"Error deleting cache dir", e);
}

Alternately, rather than deleting the whole cache directory, you may want to create subdirectories within the app's cache directory for specific data. Than you can delete those specific directories when required (e.g. on user logout).

SBerg413
  • 14,515
  • 6
  • 62
  • 88
1

From the documentation:

Saving cache files

If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files.

When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.

Create a method to recurse through the folder and delete them, if that's what you want to do.

Dan
  • 1,002
  • 12
  • 24