Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Secure Remove Files Directories Mac OS X Command Line

· 6 min read · Home Tech

In this article
  1. Why Secure Deletion Matters
  2. The Built-In Secure Commands
  3. How to Use rm -P
  4. How to Use srm (Older macOS)
  5. Critical Limitations on Modern Hardware
  6. Choosing the Right Approach
  7. Common Mistakes and Misconceptions
  8. Alternatives to Command-Line Secure Deletion
  9. Frequently Asked Questions

Yes, you can securely remove files and directories from macOS using the command line with the built-in srm command (macOS 10.12 and earlier) or by using rm with the -P flag (macOS 10.12.4 and later), which overwrites the file data before unlinking it. This article explains both methods, how they work, and the important limitations you need to know before relying on them.

Why Secure Deletion Matters

When you delete a file normally in macOS (via Finder or the rm command), the system only removes the file’s directory entry and marks the disk space as available. The actual data remains on the storage medium until it is overwritten by new files. Recovery tools can often reconstruct this data. Secure deletion attempts to prevent this by overwriting the file’s contents with meaningless data before releasing the space, making recovery significantly harder.

The Built-In Secure Commands

macOS has provided two main command-line tools for secure deletion, depending on your operating system version:

  • srm (secure remove) – Available in macOS 10.12 Sierra and earlier. It overwrites and removes files and directories.
  • rm -P – Available in macOS 10.12.4 and later. The -P flag overwrites the file three times before unlinking.

Apple removed srm from macOS in 10.12.4, replacing it with the -P option on rm. Apple’s release notes stated that the older method was not effective for all storage types, particularly solid-state drives (SSDs).

How to Use rm -P

The rm -P command is straightforward. Open Terminal and type:

  1. Navigate to the directory containing the file: cd /path/to/directory
  2. Run: rm -P filename
  3. For a directory and everything inside it: rm -rP directoryname

The -r flag means recursive (delete the directory and its contents), and -P means overwrite the file three times before unlinking. The three overwrites use specific patterns: 0xFF, 0x00, and then random data.

How to Use srm (Older macOS)

If you are running macOS 10.12 or earlier, the srm command is available:

  • srm filename – securely remove a single file.
  • srm -r directoryname – securely remove a directory and its contents.
  • srm -rf directoryname – force removal without prompting for confirmation.

By default, srm overwrites the file with random data, then zeros, then random data again. It also attempts to overwrite the file’s name in the directory entry.

Critical Limitations on Modern Hardware

Secure deletion on traditional hard disk drives (HDDs) works as intended: the overwritten sectors physically replace the old data. However, on solid-state drives (SSDs) and flash storage (including most Macs built after 2010), secure deletion is unreliable for several reasons:

  • Wear leveling: The SSD controller distributes writes across all cells to extend drive life. When you overwrite a file, the new data may be written to different physical cells than the original data, leaving the old data in place.
  • TRIM: macOS sends a TRIM command to SSDs when a file is deleted, which tells the controller to erase the blocks immediately. This actually helps, but it happens regardless of whether you use rm or rm -P.
  • Spare blocks: SSDs keep spare capacity for failed cells. Data written to these blocks is not addressable by the operating system and cannot be overwritten by any software command.

For this reason, Apple’s own guidance and most security experts agree that secure deletion of individual files on SSDs is not guaranteed. The only reliable method for an SSD is full-disk encryption (FileVault) enabled from the start, so that even if data remains, it is unreadable without the encryption key.

Choosing the Right Approach

Your choice depends on your macOS version and your storage type:

  • macOS 10.12.4 or later, HDD: Use rm -P for individual files.
  • macOS 10.12.4 or later, SSD: Do not rely on rm -P. Instead, use FileVault and simply delete files normally.
  • macOS 10.12 or earlier: Use srm if you have an HDD.
  • Any version, external SSD or USB flash drive: Treat like an internal SSD; secure deletion is not reliable.

If you need to securely erase an entire drive (not just files), use Disk Utility’s “Erase” function with Security Options, or the diskutil secureErase command. This works on HDDs but is also ineffective on SSDs for the same reasons.

Common Mistakes and Misconceptions

A frequent misunderstanding is that rm -P and srm are interchangeable. They are not – srm is gone from newer macOS versions, and rm -P does not exist on older versions. Another misconception is that secure deletion protects against all recovery methods. In reality, forensic tools with specialized hardware can sometimes recover data even from overwritten HDDs, especially if only one or two passes were used.

Another common error is forgetting the -r flag when removing a directory. Running rm -P directoryname will fail with an error message. You must use rm -rP directoryname.

Finally, note that rm -P does not overwrite the file’s name in the directory entry, nor does it clear the file’s metadata (creation date, permissions, etc.). srm attempted to overwrite the name, but this is not fully effective on modern filesystems.

Alternatives to Command-Line Secure Deletion

If the built-in commands do not meet your needs, consider these alternatives:

  • FileVault (full-disk encryption): The most reliable approach on any modern Mac. Once enabled, all data on the disk is encrypted. Deleting a file normally leaves encrypted data behind, which is useless without the password.
  • Third-party tools: Applications like Permanent Eraser or Secure Delete (for older macOS) provide a graphical interface. However, they use the same underlying system calls and face the same SSD limitations.
  • Disk Utility secure erase: For erasing an entire HDD, this is the official Apple method. It offers options for zeroing the drive or performing multiple passes.
  • Physical destruction: For truly sensitive data on a failed drive, physical destruction (shredding or degaussing) is the only guaranteed method.

Frequently Asked Questions

Does rm -P work on macOS Ventura or Sonoma?

Yes. The -P flag is still supported in current macOS versions. It works exactly as described, overwriting the file three times before unlinking. However, its effectiveness on SSDs remains limited.

Can I recover a file after using rm -P?

On a traditional HDD, recovery is extremely unlikely after the three overwrites. On an SSD, recovery may be possible because of wear leveling and spare blocks, though TRIM often erases the data quickly anyway. The only way to be certain is to have used FileVault.

Is srm still available in macOS?

No. Apple removed srm in macOS 10.12.4 (released in 2017). If you are using a newer version, the command will not be found. You can still use rm -P instead.

What is the difference between rm -P and rm -P -r?

The -r flag is required for directories. rm -P file.txt securely removes a single file. rm -rP folder securely removes the folder and all its contents recursively. Without -r, removing a directory fails with an error.

Does secure deletion work on APFS?

APFS (Apple File System) is the default filesystem since macOS High Sierra. The rm -P command still functions on APFS volumes, but APFS uses copy-on-write and snapshots, which can leave additional copies of data that are not overwritten. This further reduces the reliability of secure deletion on modern Macs.

What should I do instead of secure deletion on an SSD?

Enable FileVault before storing sensitive data. With FileVault on, deleting a file normally is sufficient because the encrypted data is unreadable without your login password. For an entire drive, you can erase it in Disk Utility, which will generate a new encryption key and discard the old one, effectively making the old data unrecoverable.