title: Android Webview - Completely Clear the Cache
body: <p>I have a WebView in one of my Activities, and when it loads a webpage, the page gathers some background data from Facebook.</p>
  
  <p>What I'm seeing though, is the page displayed in the application is the same on each time the app is opened and refreshed.</p>
  
  <p>I've tried setting the WebView not to use cache and clear the cache and history of the WebView.</p>
  
  <p>I've also followed the suggestion here: <a href="https://stackoverflow.com/questions/1790254/how-to-empty-cache-for-webview">How to empty cache for WebView?</a></p>
  
  <p>But none of this works, does anyone have any ideas of I can overcome this problem because it is a vital part of my application.</p>
  
  <pre><code>    mWebView.setWebChromeClient(new WebChromeClient()
      {
             public void onProgressChanged(WebView view, int progress)
             {
                 if(progress &gt;= 100)
                 {
                     mProgressBar.setVisibility(ProgressBar.INVISIBLE);
                 }
                 else
                 {
                     mProgressBar.setVisibility(ProgressBar.VISIBLE);
                 }
             }
      });
      mWebView.setWebViewClient(new SignInFBWebViewClient(mUIHandler));
      mWebView.getSettings().setJavaScriptEnabled(true);
      mWebView.clearHistory();
      mWebView.clearFormData();
      mWebView.clearCache(true);
  
      WebSettings webSettings = mWebView.getSettings();
      webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  
      Time time = new Time();
      time.setToNow();
  
      mWebView.loadUrl(mSocialProxy.getSignInURL()+"?time="+time.format("%Y%m%d%H%M%S"));
  </code></pre>
  
  <p>So I implemented the first suggestion (Although changed the code to be recursive)</p>
  
  <pre><code>private void clearApplicationCache() {
      File dir = getCacheDir();
  
      if (dir != null &amp;&amp; dir.isDirectory()) {
          try {
              ArrayList&lt;File&gt; stack = new ArrayList&lt;File&gt;();
  
              // Initialise the list
              File[] children = dir.listFiles();
              for (File child : children) {
                  stack.add(child);
              }
  
              while (stack.size() &gt; 0) {
                  Log.v(TAG, LOG_START + "Clearing the stack - " + stack.size());
                  File f = stack.get(stack.size() - 1);
                  if (f.isDirectory() == true) {
                      boolean empty = f.delete();
  
                      if (empty == false) {
                          File[] files = f.listFiles();
                          if (files.length != 0) {
                              for (File tmp : files) {
                                  stack.add(tmp);
                              }
                          }
                      } else {
                          stack.remove(stack.size() - 1);
                      }
                  } else {
                      f.delete();
                      stack.remove(stack.size() - 1);
                  }
              }
          } catch (Exception e) {
              Log.e(TAG, LOG_START + "Failed to clean the cache");
          }
      }
  }
  </code></pre>
  
  <p>However this still hasn't changed what the page is displaying. On my desktop browser I am getting different html code to the web page produced in the WebView so I know the WebView must be caching somewhere.</p>
  
  <p>On the IRC channel I was pointed to a fix to remove caching from a URL Connection but can't see how to apply it to a WebView yet.</p>
  
  <p><a href="http://www.androidsnippets.org/snippets/45/" rel="noreferrer">http://www.androidsnippets.org/snippets/45/</a></p>
  
  <p>If I delete my application and re-install it, I can get the webpage back up to date, i.e. a non-cached version. The main problem is the changes are made to links in the webpage, so the front end of the webpage is completely unchanged.</p>