title: How to clear Cache in iOS?
body: <h2><strong>TLDR</strong>: <strong>How, based on my current cacheManager, do I clear cache? What code should I use and how?</strong></h2>
  
  <p>I have the bellow cacheManager class which I use to cache user created videos. <a href="https://stackoverflow.com/questions/55819016/how-do-i-asynchronously-download-and-cache-videos-for-use-in-my-app">More info on how I use it is here</a>. </p>
  
  <pre><code> public enum Result&lt;T&gt; {
      case success(T)
      case failure(NSError)
  }
  
  class CacheManager {
  
      static let shared = CacheManager()
      private let fileManager = FileManager.default
      private lazy var mainDirectoryUrl: URL = {
  
          let documentsUrl = self.fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
          return documentsUrl
      }()
  
      func getFileWith(stringUrl: String, completionHandler: @escaping (Result&lt;URL&gt;) -&gt; Void ) {
  
          let file = directoryFor(stringUrl: stringUrl)
  
          //return file path if already exists in cache directory
          guard !fileManager.fileExists(atPath: file.path)  else {
              completionHandler(Result.success(file))
              return
          }
  
          DispatchQueue.global().async {
  
              if let videoData = NSData(contentsOf: URL(string: stringUrl)!) {
                  videoData.write(to: file, atomically: true)
                  DispatchQueue.main.async {
                      completionHandler(Result.success(file))
                  }
              } else {
                  DispatchQueue.main.async {
                      let error = NSError(domain: "SomeErrorDomain", code: -2001 /* some error code */, userInfo: ["description": "Can't download video"])
  
                      completionHandler(Result.failure(error))
                  }
              }
          }
      }
  
      private func directoryFor(stringUrl: String) -&gt; URL {
  
          let fileURL = URL(string: stringUrl)!.lastPathComponent
          let file = self.mainDirectoryUrl.appendingPathComponent(fileURL)
          return file
      }
  
      public func clearCache() {
          //Delete Cookies
          if let cookies = HTTPCookieStorage.shared.cookies {
              for cookie in cookies {
                  NSLog("\(cookie)")
              }
          }
  
          let storage = HTTPCookieStorage.shared
          for cookie in storage.cookies! {
              storage.deleteCookie(cookie)
          }
      }
  
  }
  </code></pre>
  
  <p>I have now recently come across a problem <a href="https://stackoverflow.com/questions/56083826/media-i-created-a-while-ago-appearing-in-new-posts?noredirect=1#comment98818979_56083826">which I asked about here</a>. </p>
  
  <p>To fix the problem i believe I need to clear cache. The problem is I dont know how to do this. </p>
  
  <p>What I tried:</p>
  
  <pre><code>    public func clearCache() {
      //Delete Cookies
      if let cookies = HTTPCookieStorage.shared.cookies {
          for cookie in cookies {
              NSLog("\(cookie)")
          }
      }
  
      let storage = HTTPCookieStorage.shared
      for cookie in storage.cookies! {
          storage.deleteCookie(cookie)
      }
  }
  </code></pre>
  
  <p>Used like this as soon as the first instance of fetching content occurs. </p>
  
  <pre><code>        CacheManager.clearCache()//Instance member 'clearCache' cannot be used on type 'CacheManager'; did you mean to use a value of this type instead?
          self.getFollowers()
  </code></pre>
  
  <p>Some info on how I use the cache manager:</p>
  
  <ul>
  <li>For each media which is fetched I use the cache manager to store the video url (which I assume is the cached videoURL) for use when needed</li>
  </ul>