Documents Directory is a directory in the iOS app sandbox designed for storing user data that should persist between app sessions and be accessible to the user through iTunes File Sharing and iCloud. According to Apple File System Programming Guide (2024), the contents of this directory are automatically included in iCloud and iTunes backups, so the developer must consciously choose which data to place in Documents. Unlike the Caches Directory, files in Documents are not deleted by the system when space is low — responsibility for managing size lies with the application.
Key Takeaways
Documents Directory is a directory inside the iOS app sandbox designed for storing user data that should persist between launches and be accessible to the user. Each app gets its own isolated sandbox, and Documents is one of the key directories alongside Caches, tmp and Library.
iOS uses a strict sandbox: an app does not have access to the file system of other apps or to system directories without special permissions. Documents Directory is the only directory whose contents the user can view via iTunes File Sharing (when the corresponding UIFileSharingEnabled key is enabled in Info.plist).
According to Apple WWDC 2023, more than 85% of apps in the App Store use Documents Directory to store at least one type of user data — from exported PDFs to saved game files and exported images.
It is important for the developer to understand: files in Documents are automatically included in iCloud and iTunes backups. If an app stores large amounts of recoverable data in Documents (e.g., image cache or temporary files), this leads to unnecessary consumption of the user's iCloud storage space.
In Swift, the path to Documents Directory is obtained through FileManager. Apple recommends using URL-based API instead of string-based for better compatibility with modern iOS capabilities.
import Foundation
let fileManager = FileManager.default
guard let documentsURL = fileManager.urls(
for: .documentDirectory,
in: .userDomainMask
).first else { return }
// Create file in Documents
let fileURL = documentsURL.appendingPathComponent("report.pdf")
let data = Data("Hello, world!".utf8)
try data.write(to: fileURL)
Objective-C uses NSSearchPathForDirectoriesInDomains — an older but still supported approach that returns a string path instead of a URL.
@import Foundation;
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES
);
NSString *documentsPath = paths.firstObject;
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"report.pdf"];
Modern projects in Swift should use FileManager.urls, since this method returns a URL rather than a string, which reduces the risk of path encoding errors and makes the code more type-safe.
Documents Directory is intended for data created by the user or explicitly needed by the user. Apple highlights several categories that are appropriate for this directory.
Files that the user creates or imports — text documents, PDFs, images, exported reports, backup files. This data has direct value to the user, and its loss would be critical.
Game saves, app state files, exported projects — everything the user expects to restore after reinstalling the app. However, for critical data it is recommended to additionally use iCloud Key-Value Storage or Core Data with iCloud sync.
| Data Type | Suitable for Documents | Alternative |
|---|---|---|
| PDF and text documents | Yes | — |
| Image cache | No | Caches Directory |
| Game saves | Yes | iCloud KVS |
| Logs and debug data | No | Caches or tmp |
| Exported reports | Yes | — |
The key criterion: if data can be re-downloaded from the network or recreated — it belongs in Caches, not in Documents. Every gigabyte in Documents is a gigabyte in the user's iCloud backup.
iOS automatically includes the contents of Documents Directory in backups when the device is connected to iTunes or when syncing with iCloud. This behavior cannot be disabled at the directory level — only per-file via the NSURLIsExcludedFromBackupKey attribute.
Starting with iOS 5.0, Apple began rejecting apps that store large amounts of recoverable data in Documents. Apple's recommendation: files that can be re-downloaded should be stored in Caches Directory with the backup exclusion flag.
import Foundation
let documentsURL = FileManager.default
.urls(for: .documentDirectory, in: .userDomainMask)
.first!
// Exclude file from iCloud backup
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
var fileURL = documentsURL.appendingPathComponent("cached_data.json")
try fileURL.setResourceValues(resourceValues)
iCloud synchronization works via NSUbiquitousContainer if the app uses iCloud Documents. In this case, files from Documents Directory are automatically synced across the user's devices. For apps without iCloud, synchronization is limited to backup.
The difference between Documents and Caches is one of the most common misconceptions among beginner iOS developers. The main difference: the system can delete files from Caches at any time to free up space, but it never touches Documents without user knowledge.
| Characteristic | Documents Directory | Caches Directory |
|---|---|---|
| iCloud backup | Yes (by default) | No |
| System deletion | Never | When low on space |
| iTunes File Sharing | Yes (when flag is enabled) | No |
| Purpose | User data | Cache, temporary data |
| Data recovery | Requires restoration | Can be re-downloaded |
According to Apple Developer Documentation (2024), improper use of Documents Directory is one of the common reasons for app rejection during review: if an app stores more than a few megabytes of recoverable data in Documents, Apple recommends moving it to Caches or applying NSURLIsExcludedFromBackupKey.
A practical rule: if the user would be upset about losing the file — store it in Documents. If the file can be re-downloaded or regenerated — store it in Caches.
Experienced iOS developers have developed several rules that help avoid problems with Documents Directory at all stages of the app lifecycle — from development to App Store publication.
Regularly check the size of Documents Directory via FileManager.enumerator(at:includingPropertiesForKeys:). If the size exceeds 100 MB for non-user data — it is a reason to reconsider the storage architecture.
For any files that can be re-downloaded, set isExcludedFromBackup = true. This reduces the load on the user's iCloud storage and decreases the risk of App Review rejection.
When changing the data format in Documents, plan for migration: do not delete old files until you are sure new ones are correctly created. Use version-specific subdirectories.
import Foundation
let documentsURL = FileManager.default
.urls(for: .documentDirectory, in: .userDomainMask)
.first!
let versionDir = documentsURL.appendingPathComponent("v2")
try FileManager.default.createDirectory(
at: versionDir,
withIntermediateDirectories: true
)
Following these practices reduces the risk of user data loss, decreases iCloud backup size, and simplifies App Store review.
Frequently Asked Questions
Yes, via Files — the built-in iOS app starting from version 11. When the UIFileSharingEnabled key is enabled in Info.plist, the contents of Documents Directory appear in the Files app under "On My iPhone". The user can view, copy and delete files.
The entire app sandbox, including Documents Directory, Caches, tmp and Library, is completely removed from the device. iCloud backups are retained until restoration or manual deletion. Upon reinstallation, the app starts with a clean sandbox.
Use FileManager.enumerator to traverse all files in the directory and sum their sizes. For each file, get the .fileSize attribute via resourceValues(forKeys:). Alternatively, use URLResourceKey.fileSizeKey and .directoryEnumerationResults.
By default, Core Data creates the SQLite file in Library/Application Support, not in Documents. Moving the database to Documents is not recommended — it will be included in iTunes File Sharing and the user could accidentally delete or modify it. The exception is if the app explicitly gives the user access to data via Core Data.
UIFileSharingEnabled (Application supports iTunes file sharing) is a boolean key in Info.plist. When set to YES, users can copy files from Documents Directory via iTunes and Files. Add the key to Info.plist: UIFileSharingEnabled = YES. Enable it only if the app actually creates user documents.
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