Clean & eject external drives in macOS

There have been a host of macOS utilities over the years that remove macOS hidden system and metadata files from external drives in order to prevent compatibility issues on devices like MP3 players, car stereos, etc.:

The developer of CleanEject kindly posted a Bash script that does the job admirably (tested in macOS Sequoia):

#!/bin/bash
if [ -d "$1/.Spotlight-V100" ]; then
   dot_clean -m "$1"
   find "$1" -name .DS_Store -o -name .apdisk -delete
   rm -rf "$1/.Trashes"
   rm -rf "$1/.Spotlight-V100"
   rm -rf "$1/.fseventsd"
   rm -rf "$1/.TemporaryItems"
   hdiutil unmount "$1"
fi

Here's a slightly modified version that does not require .Spotlight-V100 to be present, fixes .DS_Store deletion, and uses a safer eject method:

#!/bin/bash
if [ -d "$1" ]; then
  dot_clean -m "$1"
  find "$1" \( -name .DS_Store -o -name .apdisk \) -type f -delete
  rm -rf "$1"/{.Trashes,.Spotlight-V100,.fseventsd,.TemporaryItems}
  diskutil eject "$1"
fi

❧ 2025-09-01