편집

다음을 통해 공유


Clear browsing data from the user data folder

To clear browsing data from the user data folder for a WebView2 app and free up space, call the methods of the Clear Browsing Data API.

The Clear Browsing Data API allows you to programmatically erase data in the user data folder that's associated with a WebView2 user profile. For example, use this API to clear user data and history when a user signs out.

You can:

  • Clear all browsing data.
  • Clear selected kinds of browsing data.
  • Clear selected kinds of browsing data in a specified time range.

Clear all browsing data

This method clears all the kinds of browsing data that are listed in the data kinds enumeration, regardless of when the data was created. It clears the data from the user data folder for the user profile on which the method is called.

Clear selected kinds of browsing data

This method clears the specified kinds of browsing data, regardless of when the data was created. It clears the data from the user data folder for the user profile on which the method is called.

Clear selected kinds of browsing data in a time range

This method clears the specified kinds of browsing data that was created between the specified start time and end time. It clears the data from the user data folder for the user profile on which the method is called.

Example: Clearing selected kinds of browsing data in a time range

This example clears autofill data and password autosave data from the last hour.

The following parameter values are passed to the Clear Browsing Data API method:

  • The selected kinds of browser data = autofill data and password autosave data.

  • The specified time range = the past hour (3600 seconds).

// Clears autofill data.
private void ClearAutofillData()
{
    CoreWebView2Profile profile;
    if (webView.CoreWebView2 != null)
    {
        profile = webView.CoreWebView2.Profile;
        // Get the current time, the time in which the browsing data will be cleared
        // until.
        System.DateTime endTime = DateTime.Now;
        System.DateTime startTime = DateTime.Now.AddHours(-1);
        // Offset the current time by one hour to clear the browsing data from the
        // last hour.
        CoreWebView2BrowsingDataKinds dataKinds = (CoreWebView2BrowsingDataKinds)
                                 (CoreWebView2BrowsingDataKinds.GeneralAutofill | 
                                  CoreWebView2BrowsingDataKinds.PasswordAutosave);
        await profile.ClearBrowsingDataAsync(dataKinds, startTime, endTime);
    }
}

APIs:

API Reference

See also