The app cache directory is a temporary data store for files that can be recreated on next use. According to Android Developers, 2026, the system may delete files from this directory when storage is low without warning, so the app must not rely on cache persistence for critically important data. Proper use of the cache directory reduces storage footprint and speeds up content loading.
Key Takeaways
context.cacheDir and context.externalCacheDir for cache storage on internal and external memoryNSCachesDirectory, which is automatically excluded from iCloud backupsCache directory is a special directory in the app’s internal (or external) storage designed for temporary files. The key difference from Internal Storage: the system has the right to delete files from the cache without notice if the device is low on free space. Therefore, the app should never store the only copy of important user data in the cache. The cache is optimal for downloaded images, server responses, precompiled resources, and any other data that can be remotely restored or recreated programmatically.
On Android, the cache directory is located at /data/data/<package>/cache/ and is accessible via context.cacheDir. The cache size is not explicitly limited, but Google Play recommends not exceeding 100 MB, as apps with large caches receive negative user reviews. On iOS, the cache directory is inside the Sandbox container at Library/Caches/ and is accessible via NSCachesDirectory. iOS may delete files from Caches when restoring the device from a backup or when critically low on space — this should be communicated to users in the app documentation.
Understanding which data can be safely placed in the cache and which should be stored in Internal Storage or Documents is a key developer skill. Incorrect cache usage leads to two opposite problems: either the app takes up too much space (if the developer stores what should be in Documents in the cache) or the user loses data (if the developer stores in the cache what should be persisted). Follow a simple rule: if data can be recovered — cache it; if recovery is impossible — Internal Storage or Documents.
Different data types have different recreation speed and storage requirements. Understanding these characteristics helps the developer correctly choose which files to put in the cache and which in permanent storage.
The most common type of cached data — images downloaded from the network. Libraries like Glide, Picasso, and Coil automatically save downloaded images to the app’s cache directory. A typical image cache size in social apps ranges from 50 to 200 MB. The cache size depends on the device screen resolution and the amount of content viewed. Glide uses two-level caching: it first checks the L1 cache in RAM (LRU algorithm), then the L2 cache on disk. This ensures fast loading of repeatedly viewed images without an additional network request. Configuring the maximum disk cache size via DiskCacheStrategy allows control over used space: when the limit is exceeded, the library automatically removes the least recently used files.
val cacheDir = File(context.cacheDir, "image_cache")
val maxSize = 50 * 1024 * 1024 // 50 MB
val cache = DiskLruCache.open(cacheDir, 1, 1, maxSize)
cache.edit("key")?.let { editor ->
editor.newOutputStream(0).use { stream ->
// write data to cache
}
}
API response data can be cached for offline access and to reduce server load. OkHttp provides built-in caching support via the Cache class. Cache-Control and ETag response headers manage the caching policy: the server specifies how long a response is considered fresh. With proper configuration, a network request cache can reduce data loading time by 60–80% on repeat visits and provide basic app functionality without an internet connection. Network request cache size rarely exceeds 10–20 MB, but with heavy app usage it can reach 50 MB. Configure the maximum cache size via the OkHttpClient.Builder constructor and check cached data freshness on each app launch.
SQLite databases can generate temporary files during operation: WAL files (Write-Ahead Log), rollback journals, and index pages. These files are stored alongside the main database, but for temporary databases (e.g., full-text search or analytics), placement in the cache directory can be specified. Precompiled OpenGL and Vulkan shader programs are also cached in this directory, speeding up first-time graphics scene loading. On iOS, NSCachesDirectory is recommended for storing precompiled Core Data and temporary image processing files.
Cache clearing can happen automatically (by the system) or manually (by the user or app). Understanding system behavior in different scenarios is necessary to prevent data loss.
On Android, the system triggers cache clearing when free space on the /data partition drops below a critical threshold (typically 500 MB). The cacheflush process analyzes the cache size of all installed apps and removes the least recently used files, starting with the oldest. The user can also manually clear all app caches via system settings: “Settings → Storage → Cache → Clear cache.” On iOS, automatic Caches clearing occurs when restoring the device from a backup — iOS does not restore the contents of Library/Caches/. Additionally, iOS may selectively delete files from Caches when free space runs low, using the purgeable storage mechanism for isolated data.
let fm = FileManager.default
let cachesURL = fm.urls(
for: .cachesDirectory,
in: .userDomainMask
).first!
let contents = try fm.contentsOfDirectory(
at: cachesURL,
includingPropertiesForKeys: nil
)
for fileURL in contents {
try fm.removeItem(at: fileURL)
}
The developer can implement programmatic cache clearing on user request or on a schedule. On Android, to clear the app’s own cache, simply delete all files in context.cacheDir and context.externalCacheDir. On iOS, clear the contents of Library/Caches/ but do not delete the directory itself — only its contents. It is recommended to show the user the current cache size in the app settings along with a “Clear Cache” button with confirmation. According to Google Play Console, apps with a cache clear button receive 22% fewer complaints about storage space compared to apps without this feature. Cache clearing should be safe: the app must correctly handle the situation when cached files are deleted and transparently reload them on next access.
Despite having the same purpose, the implementation of cache directories on Android and iOS has significant differences. Developers need to account for these to ensure proper app operation on both platforms.
| Characteristic | Android | iOS |
|---|---|---|
| Default Path | /data/data/<package>/cache/ | Library/Caches/ |
| Access API | context.cacheDir | NSCachesDirectory |
| External Cache | context.externalCacheDir | Not available |
| Backup | Not backed up | Not backed up |
| System Clearing | When low on space | On restore from backup and when low on space |
| User Visibility | In app settings | Only when connected to a computer |
Android provides a separate external cache directory via context.externalCacheDir — it resides on the SD card (if installed) and is not deleted when the app is uninstalled. This is convenient for large media files but creates a risk of leaving junk on the memory card. iOS has no concept of an external cache: all temporary files are stored inside the Sandbox container and are guaranteed to be deleted upon uninstallation. On Android, the cache is visible to the user in app settings, and they can clear it manually. On iOS, system settings do not show the cache size of individual apps — the user can only clear the cache by deleting and reinstalling the app, unless the developer has added a clear button to the interface.
An important difference is behavior on restore. On iOS, when restoring from an iTunes or iCloud backup, the Caches directory is not restored because iOS assumes cached data will be recreated on first launch. On Android, when restoring from Google Drive, only Internal Storage is backed up — the cache remains empty after restore. In both cases, the app must work correctly with an empty cache, without showing errors or losing functionality.
Proper cache management is one of the factors influencing user experience and app rating. The following recommendations will help avoid common problems and improve user satisfaction.
context.externalCacheDir may return null if the SD card is not installed or unavailable. Always provide a fallback to internal cacheRegularly monitor cache size in app analytics. Integrate cache size metric reporting into Firebase Analytics or a similar system. If the average cache size exceeds 100 MB, optimize the caching strategy: reduce TTL for rarely used data, implement image compression before caching (WebP instead of PNG, reduce JPEG quality to 85%), use pagination for server content loading. Remember that users with 16–32 GB devices are particularly sensitive to app size: when the cache reaches 200 MB, many users start looking for a way to clear it or simply delete the app. According to a Google survey, 38% of users have deleted at least one app due to uncontrolled cache growth and storage consumption.
Frequently Asked Questions
No, clearing the cache only removes temporary files (saved images, server responses). User data (passwords, settings, databases) is stored in Internal Storage and is not affected when the cache is cleared.
Google Play recommends not exceeding 100 MB. For apps with intensive media content (social networks, messengers), up to 200 MB is acceptable provided automatic clearing is implemented and a limit is configured via a discrete cache.
Yes, iOS can delete files from Library/Caches when low on space or when restoring from a backup. The system uses a purgeable storage mechanism for automatic clearing of non-critical data.
cacheDir is located in the device’s internal memory and is deleted when the app is uninstalled. externalCacheDir resides on the SD card and may remain after uninstallation — it must be cleaned manually via code on first launch after reinstallation.
Libraries like Glide, Picasso, and Coil use two-level caching: L1 — RAM (LRU cache for instant access), L2 — disk (app cache directory). The disk cache has a configurable size limit and an old file eviction policy.
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.