Skip to content

Instantly share code, notes, and snippets.

@insidegui
Created September 14, 2016 23:12
Show Gist options
  • Save insidegui/4a5de215a920885e0f36294d51263a15 to your computer and use it in GitHub Desktop.
Save insidegui/4a5de215a920885e0f36294d51263a15 to your computer and use it in GitHub Desktop.
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
Copy link

Very useful, thank you.

@n1kron
Copy link

n1kron commented Jan 11, 2019

Thank you, very much!

@imfeemily
Copy link

You saved my time for a wholeday, Thanks :)

@mberger-livly
Copy link

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

@hlazarpesic
Copy link

You save my day! Thank you!

@JahnaviAN
Copy link

Thanks a lot. :)

@ls-rajan-rauniyar
Copy link

Thanks a lot. Cheers :)

@jclwong
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);
        }];
    }];

@tugcearar
Copy link

Thank you :)

@pradipwalghude
Copy link

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.

@hiral128
Copy link

Thank you

@elorz007
Copy link

elorz007 commented Apr 9, 2020

From a privacy point of view, I would recommend to not log the "record" object, just in case because it might leak sensitive information in your logs...

@insidegui
Copy link
Author

Definitely not. This code should not be in a production app, only in debug builds for testing. If you do ship this and want logs, use os_log so that sensitive information gets redacted.

@pnigam07
Copy link

pnigam07 commented Jun 9, 2020

How to use for WKWebView ?

@HunterT11
Copy link

thank you

@surajupreti713
Copy link

hey guys, is there a way to delete WKWebView's cache before a certain date? Like i want to only delete all the cache 15 days before.

@ngocbaomobile
Copy link

many thanks

@29satnam
Copy link

🙌

@plaa
Copy link

plaa commented Apr 8, 2021

This unnecessarily loops through the records, and does not provide a completion handler for all clearing. The following is equivalent:

  HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
  
  WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
    WKWebsiteDataStore.default().removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: records, completionHandler: {
      // All done!
    })
  }

@samthui
Copy link

samthui commented Aug 19, 2021

perfect.
Many thanks

@DioGnDev
Copy link

Thanks Brother

@1oo1
Copy link

1oo1 commented Nov 23, 2021

        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")
            }
        }

Above code doesn't work on iOS15. Below code works well.

This unnecessarily loops through the records, and does not provide a completion handler for all clearing. The following is equivalent:

  HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
  
  WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
    WKWebsiteDataStore.default().removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: records, completionHandler: {
      // All done!
    })
  }

@bdashore3
Copy link

Here's the different types of code using async/await

(Without the foreach. This works for me.)

let dataRecords = await WKWebsiteDataStore.default().dataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes())
        
await WKWebsiteDataStore.default().removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: dataRecords)

(With the foreach. Requires CollectionConcurrencyKit)

let dataRecords = await WKWebsiteDataStore.default().dataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes())

await dataRecords.asyncForEach { record in
    await WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record])
}

@JigneshVadadoriyaTR
Copy link

Thank you very much

@KovtunOleg
Copy link

Thanks man!

@shravanteegala
Copy link

Team,
Where we find all WebKit data in our IOS App storage ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment