Sub topic 13
When Results Are Missing: Checking and Rebuilding the Index
Spotlight sometimes fails to find a file you know exists. This chapter covers how to work out why and how to fix it, starting with the cheapest checks and ending with a full index rebuild.
This chapter assumes you have read the earlier chapters on the Spotlight index and metadata, on Spotlight settings, and on mdfind and mdls. You should be comfortable opening Terminal and running short commands.
Start with the symptoms
Missing results usually fall into one of a few patterns. Identifying the pattern tells you where to look.
| Symptom | Likely cause | First check |
|---|---|---|
| One file is missing, others on the same drive appear | The file is new, is in an excluded folder, or has no useful metadata | Privacy list, file location, `mdls` |
| Everything in one folder is missing | The folder is excluded in Spotlight settings | System Settings, Spotlight, Search Privacy |
| A whole category is missing (for example PDFs or Mail) | The category is switched off in Spotlight settings | Search Results categories |
| Everything on an external drive is missing | Indexing is off for that volume, or the drive is not formatted in a supported way | `mdutil -s` on the volume |
| Results are old: renamed or deleted files still show | The index is stale | Rebuild the index |
| Search is slow, or the Mac is warm and the fan is running | Indexing is still in progress | Menu bar Spotlight progress, Activity Monitor |
Work through the table from top to bottom. Most problems are settings, not damage.
Step 1: Rule out the simple causes
Before touching the index, check four things.
- Spelling and scope. A name search for
invoicewill not find a file namedRechnung.pdfunless the word appears in its contents. Try a content word, or add a kind filter such askind:pdf. - Result categories. Open System Settings and go to Spotlight. If a category such as Documents, PDF Documents, or Mail & Messages is turned off, Spotlight will never show results of that type.
- Excluded folders. In the same Spotlight pane, open Search Privacy. Any folder or disk listed there is skipped. Remove the entry if it should be searchable. Spotlight re-indexes the location after you remove it, which can take a while for a large folder.
- Waiting. A file you saved seconds ago may not be searchable yet. Indexing normally catches up quickly, but after a large copy, a macOS update, or a restore from backup it can lag for minutes or hours.
Step 2: Ask Spotlight directly with mdfind and mdls
If the settings look right, compare what Spotlight returns in the Terminal with what you see in the search window. The command line shows you the raw index without the interface's ranking and category filters.
First, check whether the index knows the file by name:
mdfind -name "invoice"If the file appears here but not in the Spotlight window, the problem is a category or ranking setting. If it does not appear here either, the index has no entry for it.
Next, look at the metadata for the specific file:
mdls ~/Documents/report.pdfA healthy file shows many attributes, such as kMDItemContentType, kMDItemDisplayName, and kMDItemFSContentChangeDate. If mdls prints mostly (null) values, or the file has no content-based attributes like kMDItemTextContent, the file was not fully imported. A file with no metadata cannot be found by content search.
You can also restrict mdfind to one folder to see whether that folder has been indexed at all:
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf'"If this returns nothing for a folder that clearly holds PDFs, that folder's index is missing or empty.
Step 3: Check the indexing status
mdutil reports and changes the indexing state of a volume. To see the state of the startup volume:
mdutil -s /The output is a short status line. The two states you will see most are:
Indexing enabled.Spotlight is indexing this volume normally.Indexing disabled.Spotlight will not index it, and searches will return nothing for files stored there.
You can check any mounted volume by giving its path, for example mdutil -s /Volumes/Backup. External drives are a common source of trouble. A drive can be mounted and readable but have indexing turned off, so everything on it is missing from results.
To turn indexing on or off for a volume, use -i:
sudo mdutil -i on /Volumes/BackupReplace on with off to disable it. Disabling indexing on a volume removes it from search, so do this only when you mean to.
Step 4: Watch for indexing in progress
If indexing is enabled but results are still incomplete, the index may simply be busy. Signs of this:
- Opening Spotlight shows a message such as "Indexing" with a progress indicator.
- The processes
mds,mds_stores, andmdworkerappear near the top of Activity Monitor's CPU tab. - The Mac is warm, and battery drains faster than usual.
This is normal after a macOS upgrade, after migrating data from another Mac, or after removing a large entry from the Privacy list. Leave the Mac plugged in and awake. Indexing can take from a few minutes to several hours depending on how many files you have and how fast the disk is. Rebuilding while an index is already running only restarts the work, so wait first.
Step 5: Rebuild the index
If settings are correct, indexing is enabled, the Mac has been idle long enough to finish, and results are still missing or stale, rebuild the index. This erases the stored index for a volume. macOS then builds a new one from the files on disk. Your files are not touched. Only the search database is deleted.
sudo mdutil -E /The -E flag erases the index and / selects the startup volume. You will be asked for an administrator password. Afterward, the search window shows indexing progress, and results fill in gradually. Applications and System Settings usually return first, while the contents of documents take longest.
To rebuild a different volume, give its path:
sudo mdutil -E /Volumes/BackupExpect extra CPU use and higher battery drain during the rebuild. Searches for file contents may be incomplete until it finishes.
An alternative from the interface
You can force a re-index of a single location without the command line. Add the folder to the Search Privacy list, wait a moment, then remove it. Spotlight discards what it knew about the folder and indexes it again. This is a smaller step than rebuilding the whole volume, so try it first when only one folder is affected.
Step 6: If a rebuild does not help
Work through these in order.
- Restart the Mac. This restarts the Spotlight services and often clears a stuck indexer.
- Check free disk space. A nearly full disk can slow or stall indexing.
- Confirm the status again with
mdutil -s /, and make sure it says indexing is enabled. - Re-check Search Privacy. A parent folder that is excluded hides everything inside it.
- Check the file itself. Files in some formats, or inside certain package bundles, may be indexed by name only. Use
mdlsto see what metadata exists. If the content attributes are missing, no rebuild will add them. - Update macOS. Indexer bugs are sometimes fixed in point releases.
If searches by name work but searches by content do not, the importer for that file type is the likely cause. Rebuilding does not change which file types have importers.
A decision flow
Common mistakes
- Rebuilding first. A rebuild takes hours on a large disk and does nothing for an excluded folder or a disabled category. Check settings before running
mdutil -E. - Rebuilding repeatedly. Each erase restarts the work. Run it once, then wait.
- Forgetting
sudo.mdutil -Eand-ineed administrator rights. Without them the command fails with a permission error. - Expecting instant results. Content search is the last part of the index to be filled in.
- Assuming the file is lost. A missing search result says nothing about the file itself. Check it in Finder before worrying.
Exercises
Exercise
A PDF in ~/Documents does not appear in Spotlight, but mdfind -name "taxes" finds it. What does that tell you, and where should you look next?
Show answer
The index contains the file, so it does not need rebuilding. The problem is in how the Spotlight window shows results, most likely a result category such as PDF Documents being turned off in Spotlight settings, or the query not matching the way you typed it. Check the Search Results categories in System Settings.
Exercise
You plugged in an external drive, and nothing on it shows up in Spotlight. Which command checks its indexing state, and which command turns indexing on? Why use these before a rebuild?
Show answer
mdutil -s /Volumes/DriveName shows the state, and sudo mdutil -i on /Volumes/DriveName turns indexing on. A rebuild with -E does not help if indexing is disabled for the volume, so you must enable it first.
Exercise
Only one project folder has stale results. What is a lighter fix than running sudo mdutil -E /?
Show answer
Add the folder to the Search Privacy list and then remove it. Spotlight discards what it stored for that folder and indexes it again, leaving the rest of the index alone.