Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clear WKWebView's cookies and website data storage, very useful during development.
import Foundation
import WebKit
final class WebCacheCleaner {
class func clean() {
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
print("[WebCacheCleaner] All cookies deleted")
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
records.forEach { record in
WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
print("[WebCacheCleaner] Record \(record) deleted")
}
}
}
}
@shyguywill

This comment has been minimized.

Copy link

shyguywill commented Dec 20, 2018

Very useful, thank you.

@n1kron

This comment has been minimized.

Copy link

n1kron commented Jan 11, 2019

Thank you, very much!

@imfeemily

This comment has been minimized.

Copy link

imfeemily commented Mar 22, 2019

You saved my time for a wholeday, Thanks :)

@mberger-livly

This comment has been minimized.

Copy link

mberger-livly commented May 16, 2019

👍 Just a note for others, Be sure to call this on the main thread!

@hlazarpesic

This comment has been minimized.

Copy link

hlazarpesic commented Jul 19, 2019

You save my day! Thank you!

@JahnaviAN

This comment has been minimized.

Copy link

JahnaviAN commented Aug 2, 2019

Thanks a lot. :)

@rajan61005co

This comment has been minimized.

Copy link

rajan61005co commented Aug 5, 2019

Thanks a lot. Cheers :)

@jclwong

This comment has been minimized.

Copy link

jclwong commented Aug 18, 2019

Objective C Implementation

    [[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:WKWebsiteDataStore.allWebsiteDataTypes completionHandler:^(NSArray<WKWebsiteDataRecord *> * _Nonnull records) {
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:WKWebsiteDataStore.allWebsiteDataTypes forDataRecords:records completionHandler:^{
            resolve(nil);
        }];
    }];
@sumeyyaarar

This comment has been minimized.

Copy link

sumeyyaarar commented Sep 20, 2019

Thank you :)

@pradipwalghude

This comment has been minimized.

Copy link

pradipwalghude commented Nov 25, 2019

Hi Guys,
I have used below code but facing issue. I have call clean function on log out but after reset password unable to receive cookies.

class func clean() {
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
print("[WebCacheCleaner] All cookies deleted")

    WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
        records.forEach { record in
            WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
            print("[WebCacheCleaner] Record \(record) deleted")
        }
    }
}

Please help me asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.