Recursively delete empty directories in Windows

Solution 1:

You can use Remove Empty Directories utility.

Alternatively you can use this one-liner batch file (from DownloadSquad):

for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"

(if used inside a batch file, replace %d with %%d)

This works because rd will not remove a directory that contains files.

Solution 2:

You can also use ROBOCOPY. It is very simple and can also be used to delete empty folders inside large hierarchy.

ROBOCOPY folder1 folder1 /S /MOVE

Here both source and destination are folder1, as you only need to delete empty folders, instead of moving other files to different folder. /S option is to skip copying(moving, in the above case) empty folders. It is also faster as the files are moved inside the same drive.

Solution 3:

Since Cygwin comes with GNU find, you can do this:

find . -type d -empty -delete

Or to avoid the noise when a folder no longer exists:

find . -type d -empty -execdir rmdir {} +

Solution 4:

The free utility EmptyFolderNuker does this fine, from a base folder of your choice. It also removes those directories only containing empty sub-directories.

Solution 5:

Hmmm... maybe even simpler solution:

for /d /r %d in (*.*) do rd "%d"

Start this from the folder you want empty folders to be deleted.

/d - will work on folders, not files /r - will recurse subdirs