The Problem with Overzealous Indexing
macOS relies on an incredibly powerful background indexing engine called Spotlight (managed by the mds and mdworker processes) to make every file on your hard drive instantly searchable. While this is great for finding lost PDFs, it is a nightmare for software developers and video editors.
If you have a massive directory containing hundreds of thousands of tiny source code files (like a node_modules folder), or a scratch disk folder where massive 4K video files are constantly being rendered and deleted, Spotlight will aggressively try to read and index every single file change. This causes the mdworker process to consume 100% of your CPU, draining your battery and causing the Mac’s cooling fans to spin out of control.
While you can exclude a folder using the System Settings GUI (Privacy & Security > Spotlight), you can execute this much faster, and script it across multiple machines, using the Terminal.
Using a Hidden System File (.metadata_never_index)
The absolute most robust way to prevent Spotlight from indexing a directory—especially a directory on an external USB drive that moves between different Macs—is to place a hidden instruction file directly inside the folder.
When the Spotlight engine scans a folder and discovers a completely empty file named exactly .metadata_never_index, it immediately aborts the scan and ignores that folder and all of its subdirectories.
- Open the Terminal application (found in Applications > Utilities).
- Use the
cd(change directory) command to navigate into the folder you want to exclude. For example, a heavy development folder:
cd ~/Projects/MassiveApp/
- Use the
touchcommand to instantly create the hidden file:
touch .metadata_never_index
Because the filename begins with a period (.), it will remain invisible in the Finder, keeping your folder looking clean while silently instructing the operating system to leave it alone.
Using the mdutil Command
If you prefer to manage indexing via the macOS metadata utility itself (rather than dropping hidden files into directories), you can use the mdutil command.
To completely disable indexing for a specific volume or path, use the -i off flag. Because this modifies system-level databases, you must use sudo.
sudo mdutil -i off /Volumes/ScratchDisk/
The terminal will return a confirmation stating Indexing disabled.
To verify the current status of a directory or drive, you can run the command with the -s (status) flag:
mdutil -s /Volumes/ScratchDisk/
How to Re-Enable Indexing
If you finish your development project and decide you actually want to be able to search for those files again, simply reverse the process.
If you used the hidden file method, delete the file:
rm ~/Projects/MassiveApp/.metadata_never_index
If you used the mdutil command, turn indexing back on using the -i on flag:
sudo mdutil -i on /Volumes/ScratchDisk/
The mdworker process will spin up and immediately begin building a fresh search index for the path.