Temporary Directory (tmp) is a directory in the iOS app sandbox for storing extremely short-term data that is only needed within the current app session. According to the Apple File System Programming Guide (2024), the system may purge tmp on every app termination or device restart. Unlike the Caches Directory, temporary files in tmp are not intended to persist between launches — developers must explicitly delete them after use to avoid wasting disk space.
Key Takeaways
Temporary Directory (also known as tmp) is a directory inside the iOS app sandbox designed for storing data whose lifetime is limited to the current session. It is the most short-lived storage among all sandbox directories.
iOS provides tmp for operations that require intermediate file storage: downloading a file before moving it to Documents, creating a temporary copy before export, buffering streaming data. After the operation is complete, temporary files must be deleted.
According to Apple Developer Documentation (2024), the tmp directory is unique because its path can change between app launches. iOS generates a new tmp path on each launch — this ensures that data from the previous session will not be accidentally read. Developers should never save the tmp path between launches.
Importantly, unlike the Caches Directory, where data can survive several launches, tmp is guaranteed not to persist data between sessions in the long term. It is the most reliable way to store truly temporary data.
In Swift, the path to tmp is obtained using the global function NSTemporaryDirectory() or FileManager.temporaryDirectory. These are the simplest APIs among all sandbox directories.
import Foundation
// Option 1: NSTemporaryDirectory()
let tempDir = NSTemporaryDirectory()
let tempFile = (tempDir as NSString)
.appendingPathComponent("export_temp.pdf")
// Option 2: FileManager.temporaryDirectory (URL)
let tempURL = FileManager.default.temporaryDirectory
let exportURL = tempURL.appendingPathComponent("export_temp.pdf")
Objective-C uses the same global function NSTemporaryDirectory(). The result is a string with the full path to the app's temporary directory.
@import Foundation;
NSString *tempDir = NSTemporaryDirectory();
NSString *tempFile = [tempDir stringByAppendingPathComponent:@"temp_data.bin"];
A key difference from other directories: NSTemporaryDirectory() does not require specifying a domain or search mask — it is a global function. However, the path may change on the next launch, so never save it in UserDefaults or any other persistent storage.
Temporary Directory is intended for a strictly defined set of scenarios. Developers often confuse tmp with Caches, placing data in tmp that should live longer than one session. Let us look at the correct use cases.
Files created during data export: a temporary copy of a report before sending it via email, a compressed archive before uploading, an intermediate format conversion file. After the export is complete, the file must be deleted.
Temporary buffers for recording audio, video, or streaming data that is processed in real time. For example, voice recording before saving to persistent storage, or video stream buffering during streaming.
| Scenario | Directory | Rationale |
|---|---|---|
| Export PDF before sending | tmp | File not needed after sending |
| Cache images | Caches | Useful between sessions |
| Download file before moving | tmp | Intermediate storage |
| Session logs | tmp | Only relevant during session |
| Editing a document | tmp | Version before saving |
If data is needed only here and now — use tmp. If it might be useful in the next session — use Caches. If data loss is unacceptable — use Documents.
iOS manages the lifecycle of tmp differently from other sandbox directories. Understanding this cycle is critical for designing proper file storage in your app.
On each launch, iOS may allocate a new path for tmp. The previous path becomes inaccessible, although the physical files may remain on disk until the device is restarted. This is why Apple strongly recommends not saving the tmp path between sessions.
According to Apple Tech Note TN2150 (2024), the system can purge tmp in the following cases: device restart, reaching disk space limits, and app offloading. Unlike Caches, tmp is not intended to store data even across several launches — it is the least reliable storage in the sandbox.
Developers must explicitly delete temporary files after completing an operation using FileManager.removeItem(at:). Undeleted files in tmp accumulate and waste disk space — iOS does not guarantee their automatic cleanup in the near future.
Comparing tmp and Caches helps developers make the right decision when choosing a directory for storage. Choosing incorrectly can lead either to premature data loss or unnecessary space consumption.
| Parameter | Temporary Directory | Caches Directory |
|---|---|---|
| Lifetime | Current session only | Between sessions (no guarantees) |
| Path changes | May change every launch | Stable path |
| Auto-cleanup | On reboot, on offload | When low on space |
| Developer deletion | Required after operation | Recommended when exceeding limit |
| Usage | Intermediate operations | Persistent cache |
A practical rule: choose tmp for data that will be deleted within seconds or minutes after creation. Choose Caches for data that is useful to keep for hours or days between sessions but can be regenerated.
Working with Temporary Directory requires discipline: since data in tmp is short-lived and its accumulation can lead to wasted space, developers should follow several key practices.
FileManager.removeItem(at:) should be called immediately after finishing an operation with a temporary file. Use defer in Swift to guarantee deletion even when an error occurs.
import Foundation
let tempURL = FileManager.default
.temporaryDirectory
.appendingPathComponent("upload_temp.dat")
defer {
try? FileManager.default.removeItem(at: tempURL)
}
// Work with temp file
try "temporary data".write(to: tempURL, atomically: true)
// ... file operation ...
// defer executes when scope exits
If data might be needed an hour after creation — save it in Caches or Application Support. tmp is intended for minute-long operations, not for storage.
Following these rules ensures that temporary files do not accumulate, disk space is used efficiently, and the app correctly handles any iOS file system cleanup scenarios.
Frequently Asked Questions
Usually the path does not change within a single session, but iOS does not guarantee it. In rare cases, the system may change tmp when the app transitions from the background to the foreground. That is why Apple recommends calling NSTemporaryDirectory() each time you need the path rather than storing it in a variable.
Files will accumulate and take up disk space. iOS may purge tmp on device restart, but until then the app will waste disk space. With significant accumulation, users may see in storage settings that the app is taking up a lot of space.
Yes, you can use DataProtectionType with NSFileProtectionComplete for temporary files containing sensitive data. However, keep in mind: if the file will only be read within the current session and then deleted, encryption may be excessive. Assess the need for encryption based on the data type.
Use ProcessInfo.processInfo.globallyUniqueString to generate a unique identifier, or UUID().uuidString. FileManager also provides the urlForCreatingTemporaryFile method, which automatically generates a unique name in tmp. This guarantees no collisions during parallel writes.
Indirectly — yes. Apple pays attention to the total amount of data the app stores on disk. If tmp contains gigabytes of undeleted files, the reviewer may reject the app. The recommended limit for tmp is no more than 100 MB at any time. Regularly clean tmp and check its size via FileManager.
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