1

My app downloading and saving some support data to 'Library/Caches' directory. According to documentation,

Use this directory to write any app-specific support files that your app can re-create easily. Your app is generally responsible for managing the contents of this directory and for adding and deleting files as needed. In iOS 2.2 and later, the contents of this directory are not backed up by iTunes or iCloud. In addition, the system removes files in this directory during a full restoration of the device. In iOS 5.0 and later, the system may delete the Caches directory on rare occasions when the system is very low on disk space. This will never occur while an app is running. However, be aware that restoring from backup is not necessarily the only condition under which the Caches directory can be erased.

So, system may clean this folder. My app's supporting data not critical, so no need to put it in Documents or somewhere else. But I want to check how it handling if 'Caches' folder will be clear suddenly.

Of course, I can just check using NSFileManager if file exist.. and just use file or re-download. But one trick here exist. My app can download some files from server in background. While file downloading, it storing at 'Caches/com.apple.nsurlsessiond/[APP_ID]' and there is no way to check if file exist (because file fully managed by SDK on this step). I added error handling for this case to NSURLSession delegate and now need to check it in 'real life'.

Is there any way to directly or indirectly force system to clear 'Caches' directories for all apps?

frankWhite
  • 1,523
  • 15
  • 21
  • `Caches/com.apple.nsurlsessiond/` will not be cleared of files that that the system is in the process of downloading in the background, and your app shouldn't be looking in that directory anyway (you will be given an opaque URL when the file is completely downloaded) – Rob Napier Sep 03 '19 at 18:01
  • But you certainly can test this if you want to. Go into the simulator directory and delete the files there. – Rob Napier Sep 03 '19 at 18:03
  • I'm not sure about this ("will not be cleared of files that that the system is in the process of downloading in the background").. This application in production and has good logging system. Analyzing some log errors from users seems like system purge this folder while some downloading not completed. – frankWhite Sep 03 '19 at 19:44

2 Answers2

0

You can delete all files from Cache folder by

    func removeNetworkCache() {
        let cachePath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0])
        let appId = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String

            do {
                let content = try FileManager.default.contentsOfDirectory(atPath: cachePath)
                for file in content{
                    let fullPath = "\(cachePath)/\(appId)/\(file)"
                    try FileManager.default.removeItem(atPath: fullPath)
                }
            } catch {
               print("ERROR DESCRIPTION: \(error)")
            }
    }
Manav
  • 2,284
  • 1
  • 14
  • 27
  • This does not work. If I print the files instead of removing them, they all print. If I remove them as well, the loop stops after the first one. This was fixed, sort of, by adding another do catch around the remove line. However, when I ran the app again, the files were still there. – Victor Engel Jan 14 '23 at 17:01
  • 1
    It worked after changing one line: let fullPath = "\(cachePath)/\(file)" I think the appID is not necessary because each app has its own sandbox. – Victor Engel Jan 14 '23 at 17:09
0

You could do it the "usual way" and just uninstall the app, or, use this terminal command: xcrun simctl uninstall booted Your.app if you're running on the simulator that is.

StackUnderflow
  • 2,403
  • 2
  • 21
  • 28
  • It's not what I need it.. In this case I will erase all application. But I need to clear only cache directory – frankWhite Sep 03 '19 at 19:47
  • Not sure how to simply erase the cache directory using xcrun, but you can uninstall and re-install to the same effect: `xcrun simctl uninstall booted Your.app && xcrun simctl install booted /Users/yourname/Library/Developer/Xcode/DerivedData/ApodApp-daykjjkrvkwbeueezdnkshfhivzg/Build/Products/Debug-iphonesimulator/YourAppName.app && xcrun simctl launch booted Your.app` – Tom Anderson Jun 14 '23 at 08:22