# Script to manage Spotlight indexing on macOS devices

In macOS and iOS devices, Spotlight is a useful tool for system-wide searches, helping users locate files, documents, applications, emails, and more. It uses a selection-based search system by indexing keywords from system files, the internet, or any application on the device. Enabling Spotlight indexing ensures accurate search results. However, since Spotlight continuously indexes in the background to keep up with system changes, it may sometimes slow down the system. Disabling Spotlight indexing can help improve CPU performance. Use Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action to deploy the following scripts to manage Spotlight indexing on macOS devices.

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

Disable Spotlight indexing 
---------------------------

Execute this script to disable Spotlight indexing:

Script to disable Spotlight indexing

\#!/bin/sh sudo mdutil -i off / 

   1

2

  \#!/bin/sh 

sudo mdutil -i off / 

   

 

  

Once the script is deployed, Spotlight will not be able to provide the expected results for the search, yet you will be able to observe related content based on the keyword searched.

Delete Spotlight folder 
------------------------

After disabling Spotlight indexing, use the following script to delete all the files and folders that are used by Spotlight. This can slightly boost the system’s performance.

Script to delete Spotlight folder

\#!/bin/sh sudo rm –rf /.Spotlight\* 

   1

2

  \#!/bin/sh 

sudo rm –rf /.Spotlight\* 

   

 

  

Upon executing the above script all the files and folders with the extension ‘.Spotlight’ will be deleted. The ‘rm’ command deletes all files and directories. By utilizing the -r and -f options, you specify whether directories should be removed recursively and forcefully, respectively, without prompting for confirmation.

Enable Spotlight indexing 
--------------------------

The following script helps you to re-enable Spotlight indexing:

Script to delete Spotlight folder

\#!/bin/sh sudo mdutil -i on / 

   1

2

  \#!/bin/sh 

sudo mdutil -i on / 

   

 

  

On deploying the above script, Spotlight starts indexing and the progress of indexing can be observed while using Spotlight.

Rebuild Spotlight index 
------------------------

In some cases, Spotlight search results can become inaccurate or incomplete, such as missing files or outdated information. Rebuilding the Spotlight index is an effective solution to restore proper functionality and ensure accurate results.
The following script helps you to re-build Spotlight indexing:

Script to delete Spotlight folder

\#!/bin/sh sudo mdutil -E / 

   1

2

  \#!/bin/sh 

sudo mdutil -E / 

   

 

  

After deploying the script, all the existing spotlight indexing will be cleared, and Spotlight will start to index again.

To rebuild the Spotlight index for specific drives, a small modification can be made to the above script as follows:

Script to delete Spotlight folder

\#!/bin/sh sudo mdutil -E /Volumes/Otherharddrive 

   1

2

  \#!/bin/sh 

sudo mdutil -E /Volumes/Otherharddrive 

   

 

  

Replace ‘Otherharddrive’ with the name of the hard drive present on the device.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.