When are files from NSCachesDirectory removed?

14,182

Solution 1

Our experience is that this folder gets cleared on app updates. It would be nice to know when exactly this folder is a candidate for being cleared. The docs describe this folder as

location of discardable cache files (Library/Caches)

The NSDocumentDirectory will not be cleared on app updates but be careful using this folder since iOS 5 now uses this folder for backing up in iCloud and your app will likely be rejected if you use this directory to store anything other then user generated content.

Solution 2

Anything which is in Library folder will NOT be deleted by the iOS during App Update.

Please check following documentation from Apple which mentions:

When a user downloads an app update, iTunes installs the update in a new app directory. It then moves the user’s data files from the old installation over to the new app directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:

Application_Home>/Documents

Application_Home>/Library

Although files in other user directories may also be moved over, you should not rely on them being present after an update.

Here is the link to this documentation: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW10

Hope this helps.

Solution 3

NSCachesDirectory can be deleted in cases of low memory

On 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, you should be aware that iTunes restore is not necessarily the only condition under which the Caches directory can be erased.

more info

If you don't want your files to be deleted you have to store them in the Documents directory, but this way:

Use the "do not back up" attribute for specifying files that should remain on device, even in low storage situations. Use this attribute with data that can be recreated but needs to persist even in low storage situations for proper functioning of your app or because customers expect it to be available during offline use. This attribute works on marked files regardless of what directory they are in, including the Documents directory. These files will not be purged and will not be included in the user's iCloud or iTunes backup. Because these files do use on-device storage space, your app is responsible for monitoring and purging these files periodically.

more info

Share:
14,182
esilver
Author by

esilver

My apps: http://www.perrystreet.com

Updated on August 07, 2022

Comments

  • esilver
    esilver almost 2 years

    In what circumstances would files in the iOS NSCachesDirectory get removed? Obviously, delete and reinstall an application. What about application upgrade? What about low disk space conditions? Anything else?

  • Ciryon
    Ciryon over 12 years
    Confirming that apps that use NSDocumentDirectory for storing content that's not user generated will be rejected.
  • respectTheCode
    respectTheCode almost 12 years
    I can also confirm that Apple does check this. If you need to store files in the NSDocumentDirectory that can be rebuilt or downloaded make sure to flag them to be skipped during backup. Apple does allow this.
  • Bazinga
    Bazinga over 11 years
    @Ciryon would it be okay to store images in NSDocumentDirectory that is being picked by the user in the camera roll?
  • Bazinga
    Bazinga over 11 years
    @respectTheCode, could you please explain a little bout your comment. Im using NSDocumentDirectory, and kinda scared of getting my app rejected. would It be good to redirect it in the NSCache folder?
  • respectTheCode
    respectTheCode over 11 years
    The NSDocumentDirectory can only be used for user created content or content that can not be recreated or downloaded.
  • Andrew Plotkin
    Andrew Plotkin over 11 years
    Can NSCachesDirectory be deleted while the app is running? While the app is backgrounded? (I have a background-audio app, so it can be actively reading from Library/Caches while running in the background.)
  • jcesarmobile
    jcesarmobile over 11 years
    I don't know, they only say in cases of low memory, so if you are using the app and get a memory warning it might delete things from caches. Notice that it will only delete them in iOS 5.0.1 or greater. Anyways, the best thing is storing the files you don't want to be deleted on NSDocumentsDirectory or NSApplicationSupportDirectory
  • Genki
    Genki over 8 years
    Low memory or low disk space?
  • jcesarmobile
    jcesarmobile over 8 years
    updated the info, it says "very low on disk space", it shows an alert to the user telling him that there is no space avaiable.