Recursively delete empty directories in Windows

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.


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.


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 {} +