Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete cache-folder of app?

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/

like image 638
safari Avatar asked Nov 30 '11 13:11

safari


People also ask

How do I empty my cache folder?

Delete the cache: The fast way with a shortcut.Press the keys [Ctrl], [Shift] and [del] on your Keyboard. A new window opens, where you can setup the options to delete the cache. Select the period "since installation", to empty the whole browser cache. Check the Option "Images and Files in Cache".

Where is app cache folder?

Open Settings and select Storage. In the resulting list, tap the Apps entry (Other Apps on Android 11 and earlier). This will take you to a list of all the apps installed on your phone. Choose the app whose cache you want to clear.

How do I clear my app cache on Android?

Open Settings, and then swipe to and tap Apps. Select or search for the app you want to clear. Tap Storage, and then tap Clear cache.


4 Answers

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();
 }
like image 60
Chirag Avatar answered Sep 20 '22 15:09

Chirag


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();
}
like image 33
neteinstein Avatar answered Sep 22 '22 15:09

neteinstein


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

like image 25
Mohamed Khaled Avatar answered Sep 21 '22 15:09

Mohamed Khaled


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).

like image 21
SBerg413 Avatar answered Sep 20 '22 15:09

SBerg413