Delete specified file from document directory
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Deleting a file from the iOS documents directory is a normal FileManager operation, but it should still be done carefully. The important parts are locating the documents directory correctly, building the exact file URL you want to remove, and handling the case where the file is already missing or the operation fails.
Find the Documents Directory Correctly
In iOS, app-specific documents live in the user domain documents directory. Use FileManager.default.urls(for:in:) to retrieve it.
Using a URL is safer than building file paths manually with string concatenation.
Build the Exact File URL
Once you have the directory, append the file name you want to remove.
For example, if the file is called notes.json, this gives you the full sandboxed file URL inside the app's document directory.
Delete the File with FileManager
The actual deletion call is removeItem(at:).
This will throw if the file does not exist or if the operation fails for another reason.
A Safer Version with Existence Check
In many apps, trying to delete a missing file should not be treated as a fatal error. Check first if that matches your use case.
This makes repeated cleanup calls idempotent from the caller's perspective.
Full Runnable Example
This example creates a file, confirms it exists, deletes it, and then confirms removal.
This is useful for verifying the behavior in a small sandboxed test harness.
Working with Subdirectories
If your file lives in a subfolder under Documents, append each path component explicitly.
Deleting the correct file becomes much easier when your code treats URLs as structured paths instead of raw strings.
When to Use Documents Versus Cache
Not every file belongs in Documents. User-created or user-visible files usually do. Temporary or re-creatable files often belong in the caches directory instead.
Deleting from Documents should therefore reflect an intentional product rule, not just a generic cleanup habit.
Common Pitfalls
The most common mistake is building a raw string path manually instead of using directory URLs and appendingPathComponent. Another is calling removeItem without deciding whether a missing file is an error or a normal case. Teams also sometimes confuse the documents directory with the bundle, then try to delete files that are not writable at runtime. Finally, deleting files from the wrong sandbox location can appear to succeed in code review but fail silently in actual app behavior if the path calculation is wrong.
Summary
- Use
FileManager.default.urls(for:in:)to locate the documents directory. - Build file paths with
URLmethods, not string concatenation. - Use
removeItem(at:)to delete the file. - Decide whether missing files should throw or be ignored.
- Keep document-directory deletion intentional, because not all app files belong there.

