Search code examples
c#.netwpfxamlwebview2

Deleting login credentials and history from WPF webview2 cache in C# .NET?


Clear the cache memory of webview2 WPF while runtime

I am trying to delete the cache memory stored by WPF webview2, I am defining the userfolder but not finding any proper method to delete the userfolder. I don't want to dispose the webview2 as it will loose some other information apart from cache memory I just want to delete the login credentials and history of webview2

     async void WebFullSCreenInitialize()
        {
            string browseExe = exePath + "\\Microsoft.WebView2";
            string cacheFolder = exePath + "\\Cache";
            var op = new CoreWebView2EnvironmentOptions("--disable-web-security ; --autoplay-          policy=no-user-gesture-required");
            var env = await CoreWebView2Environment.CreateAsync(browseExe, cacheFolder, op);
            await externalWebFullSCreen.EnsureCoreWebView2Async(env);


          
        }

Solution

  • In WPF you can clear the cache folder create by Webview2 at runtime. On specific event just call a method

    public Void clearCacheWebview() 
    {
     externalWebFullSCreen.CoreWebView2.Profile.ClearBrowsingDataAsync();
     externalWebFullSCreen.Source = new URI("your url after clearing thecache");
    } 
    

    Make sure you are using the latest webview version as CoreWebView2.Profile.ClearBrowsingDataAsync is available to latest version.