0

Is there any way to delete the cache of the app at exit as using Glide to load images in recylerview from different fragments and activities is generating a huge amount of cache to the App.(don,t want to use disk cache strategies of glide as caches are required during runtime)

Zoe
  • 27,060
  • 21
  • 118
  • 148
Frisky Coder
  • 103
  • 14
  • Are you looking to explicitly delete files added only by Glide in cache ? – Nakul Aug 29 '18 at 05:55
  • Is that possible?? – Frisky Coder Aug 29 '18 at 06:00
  • I'm not sure, I briefly checked Glide souce code, but found nothing useful. You can clear all cache with this https://stackoverflow.com/a/23908638/2930834 – Nakul Aug 29 '18 at 06:25
  • Consider taking action on this question as this is duplicate question. Marking it as duplicate. – Nakul Aug 29 '18 at 07:12
  • Possible duplicate of [Clear Cache in Android Application programmatically](https://stackoverflow.com/questions/23908189/clear-cache-in-android-application-programmatically) – Nakul Aug 29 '18 at 07:13

1 Answers1

0

If you are looking for delete cache of your own application then simply delete your cache directory and it's all done!

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        deleteDir(dir);
    } catch (Exception e) { e.printStackTrace();}
}


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();
    } else if(dir!= null && dir.isFile()) {
        return dir.delete();
    } else {
        return false;
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Jatin Bhatia
  • 248
  • 4
  • 15