5

I am using WebView in my UWP application and I want to clear the cache while closing the app, is there a way? I know I can disable cache by adding headers into my HttpRequestMessage as mentioned in this link. However, I want to be able to clear the cache upon app exit.

I did try WebView.ClearTemporaryWebDataAsync() without any success. Once something is cached it normally remains throughout the app. Any help is appreciated, thanks.

Edit : Adding code snippet

var webView = new WebView();  
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));  
await WebView.ClearTemporaryWebDataAsync(); //static method  
webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));

I expect the static method to clear cache and when I navigate to same page again its cache should be cleared. Am I doing something wrong here?

Community
  • 1
  • 1
manjunath shetkar
  • 131
  • 1
  • 1
  • 6

2 Answers2

7

In UWP (XAML) there is the ClearTemporaryWebDataAsync method, that allows to webview's cache and IndexedDB data. And there is similar method for JavaScript in MSApp - clearTemporaryWebDataAsync.

Here is code sample (based on your one) that works for me:

XAML:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
     <StackPanel>
         <WebView x:Name="webView" Width="800" Height="600"></WebView>
         <Button x:Name="refreshBtn" Content="Refresh" ></Button>
     </StackPanel>
 </Grid>

C#:

    public MainPage()
    {
        this.InitializeComponent();
        refreshBtn.Tapped += RefreshBtn_Tapped;

        webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));


    }

    private async void RefreshBtn_Tapped(object sender, TappedRoutedEventArgs e)
    {
        await Windows.UI.Xaml.Controls.WebView.ClearTemporaryWebDataAsync();
        webView.Navigate(new Uri("http://refreshyourcache.com/en/cache-test/"));
    }

When I click refresh button, cache is cleared -- I see green image.

Konstantin
  • 884
  • 6
  • 12
  • 1
    I am not able to clear the cache using this code sample of yours. I tried it on 3 different machines inlcuding a phone, I never saw the green image. Also, sometimes the app throws "Access Denied" exception on ClearTemporaryWebDataAsync call or does not return from the call at all. – manjunath shetkar Jun 16 '16 at 06:53
  • I created just a new project with the code above. Can you publish full code of your sample application so that I could reproduce it? I suppose there might be some additional dependencies. – Konstantin Jun 16 '16 at 07:59
  • I created a new project as well with same code you have posted above. It still did not work for me. You did not add any capabilities or anything extra right? – manjunath shetkar Jun 21 '16 at 15:27
  • Thanks. Adding this line work for me. Windows.UI.Xaml.Controls.WebView.ClearTemporaryWebDataAsync(); – Arjang May 09 '17 at 15:21
0

There was no possibility to do it in 8.1 according
Ten Things You Need to Know About WebView – An Update for Windows 8.1

the closest method you can use now is WebView.Refresh that "reloads the file without forced cache validation by sending a "Pragma:no-cache" header to the server."
... or just clear cache in explorer

Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25