coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.84k stars 665 forks source link

Clear all cached data - disk + memory #554

Closed MFlisar closed 4 years ago

MFlisar commented 4 years ago

Is this possible? I need this to be done globally. I know I can disable the cache for single requests like following:

iv.load(...) {
    diskCachePolicy(CachePolicy.DISABLED)
    memoryCachePolicy(CachePolicy.DISABLED)
}

But this is not enough for me. I want to delete all cached data from memory and disk and let coil start from a clean new state again.

My use case is that I load icons for apps inside my app and at some point it happens that apps do change their icons. I want to have a function to reset coil completely for such cases.

colinrtwhite commented 4 years ago

You can clear the memory cache using imageLoader.memoryCache.clear(). To clear the disk cache you'll need to get the Cache used by your ImageLoader. You can get the default Cache using CoilUtils.createDefaultCache. Then call cache.directory().delete().

MFlisar commented 4 years ago

Just if someone else also reads this, we must of course delete the cache directory including it's content, easily done with the deleteRecursively kotlin extension function on the File class, e.g. like following:

fun clearCache(context: Context, memory: Boolean = true, file: Boolean= true) {
    // 1) clear memory cache
    if (memory) {
        val imageLoader = context.imageLoader
        imageLoader.memoryCache.clear()
    }
    // 2) clear file cache
    if (file) {
        val cache = CoilUtils.createDefaultCache(context)
        cache.directory().deleteRecursively()
    }
}
orrest commented 2 years ago

Just if someone else also reads this, we must of course delete the cache directory including it's content, easily done with the deleteRecursively kotlin extension function on the File class, e.g. like following:

fun clearCache(context: Context, memory: Boolean = true, file: Boolean= true) {
    // 1) clear memory cache
    if (memory) {
        val imageLoader = context.imageLoader
        imageLoader.memoryCache.clear()
    }
    // 2) clear file cache
    if (file) {
        val cache = CoilUtils.createDefaultCache(context)
        cache.directory().deleteRecursively()
    }
}

coil-kt:coil:2.1.0, The CoilUtils.createDefaultCache() deprecated.

Coil 2.x has its own disk cache and should not use OkHttp's Cache. https://coil-kt.github.io/coil/image_loaders/

And for coil 2.x delete cache

https://stackoverflow.com/questions/71120310/how-can-i-get-a-new-image-not-cached-in-android-coil