How to clear all cached images loaded from SDWebImage?

Miles picture Miles · Oct 5, 2016 · Viewed 17.4k times · Source

I have all images loaded on my app via SDWebImage. The downloading and caching works great, but I wanted to make a button that can clear all cached images in the entire app.

I have a "Clear Cache" button as a UIButton on one of my tab bar views. How can I make it so when this button is tapped, all the cached images are removed and need to be re-downloaded?

Using Swift.

Thank you!

Answer

guidev picture guidev · Oct 5, 2016

If you want to completely clear the cache do the following:

Obj-c:

- (IBAction)clearCache:(id)sender {
    [[SDImageCache sharedImageCache]clearMemory];
    [[SDImageCache sharedImageCache]clearDisk];
}

Swift 5

SDImageCache.shared.clearMemory()
SDImageCache.shared.clearDisk()

Swift 3.0

@IBAction func clearCache(sender: UIButton) {
    SDImageCache.shared().clearMemory()
    SDImageCache.shared().clearDisk()
}