8

I've got a WebView in my app. When the user logs out of my app, I'd like to delete all cached resources the WebView may have created. Looking at an emulator, I see the following files:

/data
  /data
    /com.example.myapp
      /cache
        /webviewCache
          bunch of files here..
      /databases
        webview.db
        webviewCache.db

Is there any system call I can use to clear all the elements in /cache and /databases, or should we do that manually? I'm worried about doing it manually just because I don't know what new files WebView may leave behind in future versions of android so I won't be sure I'm really clearing everything for the user.

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
user291701
  • 38,411
  • 72
  • 187
  • 285
  • possible duplicate of [Android Webview - Completely Clear the Cache](http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache) – Reno Jul 01 '11 at 02:14
  • True, I saw that answer, but my logout page doesn't have a reference to the original WebView (lives in a diff activity). So won't have access to mWebView.clearCache(true) etc. The answers in those questions seemed home-brewed too, wondering if there's an "official" way to do it as directed by google? Hoping they would give an answer on this as it's a security issue. – user291701 Jul 01 '11 at 02:21
  • Duplicate, find right one here - http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache – Darpan Feb 25 '13 at 10:35

3 Answers3

18

try this

mWebView.clearCache(true);
mContext.deleteDatabase("webview.db");
mContext.deleteDatabase("webviewCache.db");
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
16

This is the only code that saved my day!!

 CookieSyncManager.createInstance(this);         
 CookieManager cookieManager = CookieManager.getInstance();        
 cookieManager.removeAllCookie();

My scenario:

  • Once logged in via linkedIn with the webview. The webview saves the login details. I need my app to clear this when the user signs out of the application. All the other approaches did not work for me.
amalBit
  • 12,041
  • 6
  • 77
  • 94
  • What does cookies have to do with cache ? – Nicu Surdu Oct 26 '17 at 08:25
  • It does not.. but this answer is for those apps that logs the user in based on webview and a clear cache does not log the user out. So we need to clear the cookies. Cookies are also used to store other data as well. So we need to clear the cookies as well.@NicolaeSurdu – amalBit Oct 26 '17 at 15:28
  • Clearing the cache should not logout the user. The cookies should be deleted by the logout mechanism, not from the native app. – Nicu Surdu Oct 27 '17 at 08:53
  • I used oauth 2.0 to login to linkedin. The only use of a webview for me is to get the user authenticated token. Once i got that, I use this token to do app the API calls. Now when the user wants to logout, i clear do the native apps logout mechanism. After that I just clear the cookies of my webview. Is there another way of logging out of linkedin from the webview instantly? @NicolaeSurdu – amalBit Oct 27 '17 at 09:59
  • 1
    Working Perfectly. – Vinit Saxena Dec 27 '18 at 12:47
4

Only posting here because commenting can be ugly

clearCache() will work because:

From the doc:

Clear the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used.

Reno
  • 33,594
  • 11
  • 89
  • 102
  • WebView caches seem to survive across application relaunches - is that expected? And how does one clear cached items older than certain time (like older than 1 day...) – Jasper Oct 06 '15 at 07:54