2

How to clear all images from cache using AlamofireImage. I’ve gone through following answers (posted in stack overflow) but none of them working.

How to clear AlamofireImage setImageWithURL cache
AlamofireImage cache?
AlamofireImage how to clean memory and cache


Code I’ve tried to clear cache:

UIImageView.af_sharedImageDownloader.imageCache?.removeAllImages()

I tried this also:

let sharedCache = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
URLCache.shared = sharedCache

//Clear all cookies
if let sharedCookies = HTTPCookieStorage.shared.cookies {
      for cookie in sharedCookies {
            HTTPCookieStorage.shared.deleteCookie(cookie)
      }
}


//-----------------------------------
URLCache.shared.removeAllCachedResponses()

But it can’t remove all images from cache.

Note: I’ve used AlamofireImage 3.1 to load images.

Community
  • 1
  • 1
Krish
  • 59
  • 1
  • 9
  • you can use this URLCache.shared.removeAllCachedResponses() to remove the cache – Bhupat Bheda Apr 21 '17 at 11:44
  • @BhupatBheda I tried this, Testing your suggestion with different scenarios. But it's randomly working. Sometime it requires application restart. Please suggest me more effective solution. – Krish Apr 21 '17 at 12:02

1 Answers1

8

I had the same problem and part of your solution worked for me:

UIImageView.af_sharedImageDownloader.imageCache?.removeAllImages()
UIImageView.af_sharedImageDownloader.sessionManager.session.configuration.urlCache?.removeAllCachedResponses()

Let me know if it worked for you too.

In pods, I just put

pod 'AlamofireImage'

Rafaela Lourenço
  • 1,126
  • 16
  • 33
  • swift 5 update: UIImageView.af.sharedImageDownloader.imageCache?.removeAllImages() UIImageView.af.sharedImageDownloader.session.sessionConfiguration.urlCache?.removeAllCachedResponses() – Ruslan Sabirov May 02 '22 at 13:27