Where should an iOS app put files: Documents, Application Support, Caches or tmp?
Predict first
Which of these can the system delete when storage runs low?
// A: Documents
// B: Application Support
// C: Caches
- A
- B
- C
Mental model. Documents is the filing cabinet the owner cares about. Caches is the recycling bin that the building may empty overnight.
Pick by two questions: can I rebuild it? and does the user own it? Documents holds user-created content and is backed up. Application Support holds data the app needs but the user never sees, also backed up. Caches and tmp hold anything you can recreate: they are not backed up and the system may delete them when space is low, with no warning.
Get the folder with FileManager.urls(for:in:), never a hard-coded path (the app container path changes between installs). Application Support does not exist yet on a fresh install, so create it. Large re-downloadable files you keep there should be marked isExcludedFromBackup so they do not bloat backups.
Caches is for rebuildable data, so the system treats it as disposable. Documents and Application Support are protected.
| Documents | Application Support | Caches | tmp | |
|---|---|---|---|---|
| Backed up | yes | yes | no | no |
| System may delete | never | never | yes, low space | yes, when app is closed |
| Good for | user-created files | app database, config | downloads you can refetch | scratch files |
Common traps
- Saving user data in Caches. The system can wipe it, and the user loses it.
- Hard-coding a container path. It differs per install and per device.
They might follow up with
- What about shared data between app and extension? An App Group container, via
containerURL(forSecurityApplicationGroupIdentifier:). - Is tmp cleared on every launch? No guarantee. The system clears it when the app is not running; clean it yourself.
Related questions
- 2.3.2 What is the difference between a file URL and a web URL, and URL(fileURLWithPath:) and URL(string:)?
- 2.3.7 How do you read and write files safely, and why is fileExists-then-act a bug?
← What is Data, and how do you convert it to and from text? How do you read and write files safely, and why is… →