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.:

Bash

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

AppleScript

The modified version wrapped in AppleScript for a simple GUI volume chooser (assuming the user can be trusted not to select Macintosh HD, Time Machine volumes, etc.):

set selectedVolume to choose folder with prompt "Select drive to clean and eject:" default location (POSIX file "/Volumes")
set volumePath to POSIX path of selectedVolume
do shell script "
if [ -d '" & volumePath & "' ]; then
  dot_clean -m '" & volumePath & "'
  find '" & volumePath & "' \\( -name .DS_Store -o -name .apdisk \\) -type f -delete
  rm -rf '" & volumePath & "'/{.Trashes,.Spotlight-V100,.fseventsd,.TemporaryItems}
  diskutil eject '" & volumePath & "'
fi"

To reduce the risk of mishap, this final attempt ("One more coruscation, my dear Watson—yet another brain-wave!") processes only FAT and exFAT-formatted volumes:

set selectedVolume to choose folder with prompt "Select drive to clean and eject:" default location (POSIX file "/Volumes")
set volumePath to POSIX path of selectedVolume

-- Only allow FAT family and exFAT
set fsType to do shell script "diskutil info -plist " & quoted form of volumePath & " | plutil -extract FilesystemType raw -"

if fsType is not in {"msdos", "exfat", "fat16", "fat32"} then
    display alert "Unsupported Volume" message "Only FAT16, FAT32 and exFAT volumes are supported for safety" buttons {"OK"} default button "OK"
    return
end if

-- Clean the volume
do shell script "
dot_clean -m " & quoted form of volumePath & " 2>/dev/null || true
find " & quoted form of volumePath & " \\( -name .DS_Store -o -name .apdisk \\) -type f -delete 2>/dev/null || true
rm -rf " & quoted form of volumePath & "/.Trashes " & quoted form of volumePath & "/.Spotlight-V100 " & quoted form of volumePath & "/.fseventsd " & quoted form of volumePath & "/.TemporaryItems 2>/dev/null || true
"

-- Eject
try
    do shell script "diskutil eject " & quoted form of volumePath
on error
    display alert "Eject Failed" message "Cleaning completed but eject failed" buttons {"OK"} default button "OK"
end try

Related

❧ 2025-09-01