Temporary Directory — What It Is, Temporary Files, and Storage Management

Author: IT Sectr Published: 2026-07-10 Reading time: 10 min

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 — a directory for data needed only in the current app session.
  • iOS may purge tmp on device restart or app termination.
  • Files in tmp are not included in iCloud and iTunes backups.
  • The path to tmp is obtained via NSTemporaryDirectory() in Swift or Objective-C.
  • Developers must manually delete files from tmp after use.

What Is Temporary Directory in iOS?

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.

How to Get the Path to Temporary Directory

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.

swift
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.

objective-c
@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.

What Data to Store in tmp

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.

Intermediate Files During Export

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.

Buffering Streaming Data

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.

ScenarioDirectoryRationale
Export PDF before sendingtmpFile not needed after sending
Cache imagesCachesUseful between sessions
Download file before movingtmpIntermediate storage
Session logstmpOnly relevant during session
Editing a documenttmpVersion 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.

Lifecycle of Temporary Files

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.

Temporary Directory vs Caches Directory

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.

ParameterTemporary DirectoryCaches Directory
LifetimeCurrent session onlyBetween sessions (no guarantees)
Path changesMay change every launchStable path
Auto-cleanupOn reboot, on offloadWhen low on space
Developer deletionRequired after operationRecommended when exceeding limit
UsageIntermediate operationsPersistent 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.

Best Practices for Working with tmp

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.

Always Delete Files After Use

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.

swift
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

Do Not Use tmp for Long-Lived Data

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

Can the path to Temporary Directory change while the app is running?

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.

What happens if I do not delete files from tmp?

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.

Is tmp suitable for encrypting temporary data?

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.

How do I create a unique file name in tmp without collisions?

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.

Does the size of tmp affect App Store review?

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

  • Temporary Directory (tmp) — a directory for data whose lifetime is limited to the current app session.
  • The path to tmp can change between launches — do not save it in persistent storage.
  • The system can purge tmp on device restart or app offload.
  • Files in tmp are not included in iCloud and iTunes backups.
  • Unlike Caches Directory, tmp is not designed for storage between sessions.
  • Developers must explicitly delete temporary files using FileManager.removeItem.
  • Use defer in Swift to guarantee temporary file deletion regardless of the operation outcome.

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.

Discuss the project

Read also