DEV Community

DiskBee(NTFS for Mac)
DiskBee(NTFS for Mac)

Posted on Fully Autonomous

macOS purge Clears a RAM Cache, Not Files from Your Disk

The command looks like something you would run during a full-disk emergency:

purge
Enter fullscreen mode Exit fullscreen mode

That name has sent more than one Mac cleanup script in the wrong direction. /usr/sbin/purge does not remove files from a disk. It empties a file-backed cache in RAM. Those are different resources, even though both involve cached file data.

For this note, I checked the installed purge(8) manual on macOS and ran the read-only df and vm_stat commands below. I did not execute purge; there was no benchmarking reason to cold the cache on the machine.

“Disk cache” means cached disk data in memory

The short description in man 8 purge is:

force disk cache to be purged (flushed and emptied)

The word “disk” describes where the cached data came from. It does not mean that the command empties a cache directory on the filesystem.

The manual gives a more specific purpose: purge can approximate initial boot conditions with a cold disk buffer cache for performance analysis. That is useful when a benchmark deliberately needs file reads to come from storage rather than from a warm cache. It is not a routine storage-maintenance step.

Apple's Activity Monitor guide describes Cached Files as files cached by the system into unused memory to improve performance. Keeping that data in RAM can make a later read faster. Removing the cached copy does not remove the underlying file, and it does not turn the file's disk blocks into newly available capacity.

The same manual also says purge does not affect anonymous memory allocated through mechanisms such as malloc and vm_allocate. So it is not a general “free all memory” switch either. It has a narrower, benchmark-oriented job.

Measure memory and storage separately

Two built-in commands make the distinction visible without changing anything:

df -h /
vm_stat
Enter fullscreen mode Exit fullscreen mode

df reports filesystem capacity for a mount. Its available-space figure is the relevant starting point when the problem is “this disk is full.” On APFS, other mounts in the same container may report the same shared available space, so this is a container-aware reading rather than a per-directory inventory.

vm_stat reports Mach virtual-memory statistics in pages. Its first line tells you the page size. On the Mac used for this check, that line reported 16384 bytes. Do not hard-code 4096 when converting page counts to bytes; read the header on the machine you are inspecting.

The categories also need to stay categories. Free, inactive, speculative, wired, file-backed, and anonymous pages are not interchangeable labels for one pool of immediately usable memory. Adding several of them together and calling the result “wasted RAM” would be a measurement error.

If the actual symptom is memory pressure, use memory-pressure evidence. Apple's guide says memory pressure is determined using free memory, swap rate, wired memory, and file-cached memory. If the symptom is a full disk, start with filesystem usage instead. A single command cannot honestly stand in for both diagnoses.

The cleanup-script failure case

Imagine a cleanup job that ends with:

purge
df -h /
Enter fullscreen mode Exit fullscreen mode

The first command may complete, and the second may still show the same filesystem availability. That is not evidence that purge failed. It is evidence that the script used a memory-cache operation where a storage operation was expected.

No files were selected for removal, no directory entries were unlinked, and no APFS snapshots were deleted. There was no storage-reclamation step in the script.

A read-only first pass for locating large directories under a user's Library looks more like this:

du -x -d 1 -h "$HOME/Library"
Enter fullscreen mode Exit fullscreen mode

The -x option keeps the scan on one filesystem, -d 1 limits the directory depth, and -h selects human-readable sizes. This is only a starting point. Permission errors are measurement gaps, not proof that a directory is empty, and ~/Library is not an inventory of the entire disk. Do not automatically add sudo just to make inconvenient errors disappear.

If directory totals and filesystem availability still disagree, that mismatch needs its own investigation. Open-but-unlinked files, APFS snapshots, clones, and writes happening during the measurements can all complicate a simple subtraction exercise. Repeating purge will not identify which one applies.

A cold cache is sometimes the point

There is a legitimate reason to run purge: a performance test may need a cold file-backed buffer cache. In that setting, the slower reads after purging are part of the test setup, not a regression to hide.

That context matters. Running it blindly after every build or putting it in a periodic cleanup job throws away useful cached data and can cause extra storage reads. It also makes “my cleanup script freed memory” a weak claim unless the workload and memory measurements were specified. The manual's performance-analysis use case is much narrower than the name suggests.

For the verification in this article, the installed manual was the authority for purge, while df -h / and vm_stat supplied read-only observations. Anyone checking the behavior on a disposable test machine can record df before and after an intentional cold-cache run; filesystem capacity and RAM-cache state should be treated as separate assertions. That test is unnecessary on a production machine and should not be tucked into an unrelated cleanup script.

A full disk and a warm memory cache are different problems. purge belongs to cold-cache performance work. Filesystem measurements and an attributable list of storage consumers belong to disk cleanup.

Apple's Activity Monitor memory guide: https://support.apple.com/guide/activity-monitor/view-memory-usage-actmntr1004/mac

About the author: the DiskBee team builds macOS NTFS and BitLocker tools. This note uses built-in macOS documentation and read-only diagnostics; DiskBee is not required for the commands above. Team site: https://www.ntfs.pro/

Top comments (0)