title: Android WebView - clear cache
body: <p>I have a WebView control and I want to show every time a current site, but I think the WebView show only the cached version of the site. I tried everything found <a href="https://stackoverflow.com/questions/14392414/android-webviews-clearcache-is-very-slow">here</a> but nothing seems to work. I tried also to delete all files from the context.getCacheDir(), but without success. What can I do?</p>
  
  <p>This is my code from onCreate method:</p>
  
  <pre><code>    clearCache(this, 0);
  
      setContentView(R.layout.webview);
  
      deleteDatabase("webview.db");
      deleteDatabase("webviewCache.db");
  
      mWebView = (WebView)findViewById(R.id.webview);
      mWebView.setWebViewClient(new CenterWebViewClient(this));
      mWebView.setWebChromeClient(new CenterWebChromeClient(this));
      mWebView.getSettings().setJavaScriptEnabled(true);
      mWebView.getSettings().setAppCacheEnabled(false);
      mWebView.getSettings().setAppCacheMaxSize(1);
      mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
  
      String  url = "https://www.exacmple.com";
  
      if (url != null) {
          Map&lt;String, String&gt; noCacheHeaders = new HashMap&lt;String, String&gt;(2);
          noCacheHeaders.put("Pragma", "no-cache");
          noCacheHeaders.put("Cache-Control", "no-cache");
          mWebView.loadUrl(url, noCacheHeaders);
      }
  </code></pre>
  
  <p>clearCache function:</p>
  
  <pre><code>static int clearCacheFolder(final File dir, final int numDays) {
  
      int deletedFiles = 0;
      if (dir!= null &amp;&amp; dir.isDirectory()) {
          try {
              for (File child:dir.listFiles()) {
  
                  //first delete subdirectories recursively
                  if (child.isDirectory()) {
                      deletedFiles += clearCacheFolder(child, numDays);
                  }
  
                  //then delete the files and subdirectories in this dir
                  //only empty directories can be deleted, so subdirs have been done first
                  if (child.lastModified() &lt; new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                      if (child.delete()) {
                          deletedFiles++;
                      }
                  }
              }
          }
          catch(Exception e) {
              Log.e("WebView", String.format("Failed to clean the cache, error %s", e.getMessage()));
          }
      }
      return deletedFiles;
  }
  
      public static void clearCache(final Context context, final int numDays) {
           Log.i("WebView", String.format("Starting cache prune, deleting files older than %d days", numDays));
           int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
           Log.i("WebView", String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
      }
  </code></pre>