SDWebImage clearing cache

33,572

Solution 1

SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];

Don't forget to put these lines of code in your didReceiveMemoryWarning, too.

Solution 2

Located the source if an issue. It seems that I was deceived by the Singleton pattern used in SDImageCache. The cache for extension that is used over UIImageView is being controlled by SDWebImageManager which has an instance variable of SDImageCache. If you want to clear the cache for extension you have to call its imageCache's methods like clearDisk and clearMemory.

Solution 3

Only following code worked for me : Swift 5.0, Xcode 11, iOS 13, SDWebImage pod 5.0

 SDWebImageManager.shared.imageCache.clear(with: .all) {
        print("deleted all")
 }

where you choose options like SDImageCacheType.disk, SDImageCacheType.memory, SDImageCacheType.disk

Also if you want to remove specific image from cache use following:

SDWebImageManager.shared.imageCache.removeImage(forKey: "url of image", cacheType: .all)
Share:
33,572
Eugene
Author by

Eugene

swift, obj-c, react-native, reactjs, python, django, some basic nginx, uwsgi and bash scripting

Updated on January 12, 2022

Comments

  • Eugene
    Eugene over 2 years

    I'm displaying a list of icons downloaded from the web with text in a table view. The icons can be changed on server side and I need to replace them as soon as new icons are getting available. I try using the following code:

    [imgView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"table_avatar_icon"] options:SDWebImageCacheMemoryOnly];
    

    And call [[SDImageCache sharedImageCache] clearMemory]; In my refresh callback, but it does not purge the contents of the cache. More to it, even if I close the application and open it again the image is still there.

    I found only one way to clear the cache and it is by calling [[SDImageCache sharedImageCache] clearDisk];. Which only works after I close and reopen the app.

    How can I force SDWebImage to not to use disk caching?

  • Suraj Shinde
    Suraj Shinde almost 11 years
    not able to call clearcache method. [[SDWebImageManager sharedManager] clearCaches] . i tried this but it is crashing. please help me.
  • Eugene
    Eugene almost 11 years
    @SurajShinde There's no such method for SDImageCache, of course it will crash. Use [[[SDWebImageManager sharedManager] imageCache] clearDisk];, [[[SDWebImageManager sharedManager] imageCache] clearMemory];
  • Suraj Shinde
    Suraj Shinde almost 11 years
    Thanks, Actully i was using old SDwebcache it did not have imageCache instance.
  • Tomas Andrle
    Tomas Andrle almost 11 years
    The current version of SDWebImage will automatically clear the memory cache when memory warnings occur. No need to do this and especially no need to clear disk cache.
  • Carmen
    Carmen almost 11 years
    can you please add a source. Which version will support these functionality exactly?
  • Rizon
    Rizon over 8 years
    better to use the non-blocking version - clearDiskOnCompletion:
  • Artem M
    Artem M about 8 years
    No need for this, the marked answer is doing the same thing. imageCache instance variable of SDWebImageManager just returns [SDImageCache sharedImageCache]. Look at the createCache method: github.com/rs/SDWebImage/blob/master/SDWebImage/…
  • Iulian Onofrei
    Iulian Onofrei over 7 years
    @Rizon, clearDisk is not blocking! The only difference is that it doesn't have a completion callback.
  • Виктор Иванов
    Виктор Иванов about 7 years
    Update for the latest release: SDImageCache *imageCache = [SDImageCache sharedImageCache]; [imageCache clearMemory]; [imageCache clearDiskOnCompletion:^(){}];