Caches Directory is a directory in the iOS app sandbox designed for storing temporary data that can be restored or reloaded from the network. According to Apple File System Basics (2024), the system may delete files from the Caches Directory at any time to free up disk space — the app must properly handle the absence of these files and restore them when necessary. Unlike the Documents Directory, data from Caches is not included in iCloud and iTunes backups, which reduces the load on the user’s cloud storage.
Key Takeaways
Caches Directory is a directory inside the iOS app sandbox optimized for storing data that can be restored when needed. Unlike the Documents Directory, Caches is not intended for user data — it is temporary storage to speed up app performance.
iOS uses the Caches Directory to store cached network responses, preloaded images, serialized objects, and data that the app can restore. Developers should not rely on long-term data storage in this directory.
According to Apple WWDC 2020, about 40% of iOS apps use the Caches Directory to store cached images and network data, while 25% of developers incorrectly place data in Caches that should be in Documents or Application Support due to misunderstanding the differences between these directories.
A critical property of Caches: the app must properly handle situations when a cache file has been deleted by the system. If removing the cache breaks app functionality, then the data is stored in the wrong directory.
In Swift, the path to the Caches Directory is obtained using the standard FileManager method with .cachesDirectory. This is a simple operation used in virtually every iOS app that works with network data.
import Foundation
let fileManager = FileManager.default
guard let cachesURL = fileManager.urls(
for: .cachesDirectory,
in: .userDomainMask
).first else { return }
// Save cached JSON
let cacheFile = cachesURL.appendingPathComponent("feed_cache.json")
let jsonData = try JSONSerialization.data(
withJSONObject: response,
options: [.prettyPrinted]
)
try jsonData.write(to: cacheFile)
Objective-C uses NSSearchPathForDirectoriesInDomains with NSCachesDirectory. Although Apple recommends the Swift API, Objective-C code with Caches Directory remains functional and supported.
@import Foundation;
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSCachesDirectory,
NSUserDomainMask,
YES
);
NSString *cachesPath = paths.firstObject;
NSString *cacheFile = [cachesPath stringByAppendingPathComponent:@"feed_cache.plist"];
Swift projects should prefer the URL-based API: it is type-safe and integrates better with modern frameworks like SwiftUI and Combine.
Caches Directory is optimal for several categories of data that the app uses to speed up performance, but it is not the single source of truth. Choosing the right data for caching directly affects UX and app performance.
JSON responses from APIs, news feed data, object lists — anything the app can re-download from the server. Use URLCache for automatic HTTP response caching or manually save serialized objects.
Images downloaded from the network are the most common use case for the Caches Directory. Libraries like SDWebImage and Kingfisher save cached images in Caches by default.
| Data Type | Suitable for Caches | Retention Period |
|---|---|---|
| JSON API responses | Yes | Until system cleanup |
| Images from network | Yes | Until system cleanup |
| Debug logs | Conditionally | Better in tmp |
| Game saves | No | Documents only |
| App configurations | No | Application Support |
If data cannot be restored, it does not belong in Caches. This is the simplest criterion: imagine that tomorrow the system will delete all files from Caches. If the app continues to work correctly, the data is stored properly.
iOS automatically manages Caches Directory cleanup, but the exact triggers and algorithms are not documented by Apple. It is known that the system can delete files from Caches when disk space is low, as well as when the Offload Unused Apps feature is active.
The cleanup process is transparent to the app: the system deletes files without notification. The app must check for file existence before reading and recreate it if absent. Not relying on long-term storage is a key requirement when working with Caches.
According to Apple’s article “File System Basics” (2024), the app should not expect files in the Caches Directory to be available between sessions. Developers are advised to implement a fallback mechanism: if a cached file is missing, download the data from the network and save it to Caches again.
A separate scenario is app offloading. When this feature is activated, iOS removes the app but keeps its Documents Directory. The Caches Directory is deleted in the process. A user who restores the app will not have cached data — the app must download it again.
The difference between Caches and Temporary (tmp) directories often causes confusion among developers. Both directories store temporary data, but with different lifetime guarantees and purposes.
| Characteristic | Caches Directory | Temporary Directory |
|---|---|---|
| Lifetime | Session to session (not guaranteed) | Within a single session only |
| System cleanup | When space is low | When session ends or device reboots |
| Purpose | Cache to speed up performance | Very temporary data |
| Example | Cached images | Temp file before export |
| Backup | No | No |
Choose Caches if it is beneficial to keep data between app launches but it can be restored. Use tmp if data is only needed within the current session and has no value after the app terminates.
Working with the Caches Directory requires following several rules that help avoid data loss, unexpected app behavior, and performance issues.
FileManager.fileExists(atPath:) should be called before every read from Caches. If the file is missing, load the data from the source and save it to cache. Never assume a file in Caches exists.
Set a maximum size for the Caches Directory in your app. For example, a limit of 50 MB for images and 10 MB for JSON responses. When the limit is exceeded, delete the oldest files by modification date.
import Foundation
func trimCache(to maxSizeBytes: Int) {
let cachesURL = FileManager.default
.urls(for: .cachesDirectory, in: .userDomainMask)
.first!
guard let enumerator = FileManager.default
.enumerator(
at: cachesURL,
includingPropertiesForKeys: [.fileSizeKey, .contentModificationDateKey]
)
else { return }
// Enumerate and remove old files
// when exceeding size limit
}
Following these practices ensures that the app works correctly regardless of system cache cleanup actions, and users do not encounter unexpected data loss.
Frequently Asked Questions
No, iOS does not send notifications before deleting files from Caches. The cleanup process is completely transparent to the app. The only way to find out about a deletion is when trying to read a file — FileManager returns nil or throws an error, and the app must handle this situation.
Users do not have direct access to Caches Directory through Files or iTunes. However, they can clear the cache of all apps via Settings > General > Storage, select a specific app and tap “Offload App.” iOS may also automatically clear cache when storage is low.
URLCache is a built-in HTTP request caching mechanism from Foundation. It automatically saves and loads cached responses, using the Caches Directory under the hood. Manual saving gives more control: you can choose the format, encrypt data, and manage each file’s lifetime individually.
When the app is updated via the App Store, the Caches Directory is preserved. However, the contents may be deleted by the system if the new update requires more space for installation. Developers should not rely on Caches persistence after an update — this is an additional reason to implement a fallback mechanism.
Set URLCache to nil for a specific NSURLSession or use the .reloadIgnoringLocalCacheData caching policy. You can also create a URLSessionConfiguration with an empty cache: sessionConfiguration.urlCache = nil. This is useful for data that must always be up to date.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also