admin管理员组

文章数量:1399915

I want to clear my Android app's cache programmatically, similar to how we do it from Settings > Apps > App Info > Storage & Cache > Clear Cache, but without deleting any app data. I've tried using the following function:

fun cleanCache(context: Context) {
    try {
        context.cacheDir.deleteRecursively()
        context.externalCacheDir?.deleteRecursively()
        context.toast("Cache cleared successfully")
    } catch (ignore: Exception) {
        context.toast("Failed to clear Cache")
    }
}

Additionally, I have cleared: WebView cache, Image loader memory cache, External caches.

Even after running this function, the cache is cleared but not entirely like when clearing it manually from system settings. How can I clear only the app cache (without affecting app data) exactly like the system "Clear Cache" button in App Info?

Questions: Is there a proper way to programmatically clear all app cache, similar to the system’s "Clear Cache" button? Are there any system permissions or APIs required to ensure complete cache clearing?

Any guidance or alternative approaches would be appreciated!

本文标签: kotlinHow to Clear App Cache in Android (Like in App Info) Without Deleting DataStack Overflow