How can I delete empty folders in Mac OS X?

First of all note that deleting empty directories usually isn't necessary. Some services or applications might even need certain directories to exist. Be aware of what you're doing.

You can list empty folders with find, when run from Terminal.app:

find . -type d -empty

By default find lists files and folders, but here, -type d restricts it to directories, and the -empty option shows only empty directories. This will recurse all folders descending from your home directory (/Users/your-username/ or short, ~). To expand this to your entire filesystem, use:

find / -type d -empty

Here, / signifies the root of your Mac OS X file system. You can of course use any other starting point as well, for example your external disk mounted under /Volumes/your-disk-name;

find /Volumes/your-disk-name -type d -empty

Now, if you want to delete whatever find outputs, simply append -delete, like so:

find . -type d -empty -delete

Note: This will not ask for confirmation. It'll delete all the directories it can, i.e. the ones where you have permissions to delete. They will not be moved to the trash but gone forever. If you want to be asked before removal, change the command to something like the following:

find . -type d -empty -exec rm -ri '{}' \;

I've made a small and free program that solves this better:

http://www.macupdate.com/app/mac/52551/find-empty-folders

The advantage of this program is that it also finds folders that are apparently empty but contain the invisible ".DS_Store" file.

And it also lets you move the found items to Trash right away.


To further this effort:

I created a script I use to clean my documents folder from time to time, as I am way to OCD and tired of being APP Overloaded & like simplicity.

I made this in an effort to improve & provide an alternate solution.

Lastly, for @kenche's Icon File, that doesn't seem to exist on my mac as I have not modified my folder icons, but you can in the inspector. Upon dragging a picture to the top left it will create the Icon^M file on that directory.

To find those too you can run:

(Should you be paranoid about if finding a false positive then use:ctrl+v ctrl+m instead of the ?)

find ~/Documents -type f -name 'Icon?' -print;
# and to remove 
find ~/Documents -type f -name 'Icon?' -print -delete;

IMPORTANT

Please as @slhck stated above: Some services or applications might even need certain directories to exist. This also applies to the DS_Store & Icon files, PLEASE Be aware of what you're doing. Also Note: This script will not ask for confirmation. It will delete all the directories it can. i.e. the ones where you have permissions to delete. They will not be moved to the trash but gone forever.

BASH Script

#!/bin/bash
# =============================================================================
# MAC OSX HIGH SIERRA 10.13.4 (17E199)
# Terminal: Version: 2.8.2 64-Bit (Intel): Yes
# Terminal Location: /Applications/Utilities/Terminal.app
# =============================================================================
# Terminal CLEAN UP YOUR DOCUMENTS FOLDER.
# =============================================================================
# START WHAT IS BELIEVED TO BE EMPTY NOW.
# =============================================================================
echo 'Searching Documents for empty folders...'
find ~/Documents -type d -empty -print;
# =============================================================================
# SHOW & THEN REMOVE ALL MAC OS DS_Store FILES
# =============================================================================

echo 'Searching Documents for DS_Store files...'
find ~/Documents -type f -name ".DS_Store" -print;

echo 'Removing DS_Store files...'
find ~/Documents -type f -name ".DS_Store" -print -delete;
# =============================================================================
# SHOW & THEN REMOVE ALL MAC OS ZERO SIZED FILES
# =============================================================================
echo 'Searching Documents for ZERO file sized files...'
find ~/Documents -type f -empty -print;

echo 'Removing ZERO file sized files...'
find ~/Documents -type f -empty -delete -print;
# =============================================================================
# SHOW & THEN REMOVE Icon^M FILES
# USE THE ? MARK FOR EASE OF USE YOU CAN ALSO SUB 'CTRL + V & CTRL + M' FOR ^M 
# =============================================================================

echo 'Searching Documents for Icon files...'
find ~/Documents -type f -name 'Icon?' -print;

echo 'Removing Icon files from Documents...'
find ~/Documents -type f -name 'Icon?' -print -delete;

# SEEMINGLY THE SAME AS
# find ~/Documents -type f -size 0 -print
# find ~/Documents -type f -size 0 -print -delete
# =============================================================================
# SHOWCASE NEW FOUND EMPTY FOLDERS
# =============================================================================
echo 'Showcasing new result of existing and new found empty folders...'
find ~/Documents -type d -empty -print;

echo 'Deleting result of empty folders...'
find ~/Documents -type d -empty -delete -print;

echo 'Showcasing the removal of said, 'empty folders'...'
find ~/Documents -type d -empty;

End of script.

Script can be seen here on GitHub

References:

About the icons that represent files on your Mac

Any way to remove all folders that contain only .DS_Store recursively?

Find folders & subfolders containing only ".DS_Store"

How to find all zero-byte files in a directory including subdirectories

Icon? file on OS X desktop