How can I recursively delete empty folders? [duplicate]

Posted on


How can I recursively delete empty folders?

“Empty” is considered to be true if the directory has no directory and no files exist except for useless auto-generated files (Thumbs.db, .DS_Store, etc.)


Here is a command line one-liner that will delete every actually empty (i.e. zero files) directory in the folder and below:

for /f "delims=" %i in ('dir /ad /s /b') do @rd "%i"

Basically this gets a recursive listing of all directories starting from the current and then attempts to remove each directory. The rd command will not remove a non-empty directory by default so your files should be safe.

Try this one: Remove Empty Directories – CNET

Empty directory folders are annoying, but not often a cause for great concern. Still, they build up over time, and Remove Empty Directories is a smart and fast solution. The tiny program greets you with a mostly blank pane and an icon key to understanding your results. There’s a dialog box at the top to specify a particular folder or drive to search, although by default it checks your C drive.

Here is me removing an empty directory:

C:>md f  
C:>rmdir f  

There, I created one, then removed it. If there are things in it then it is not empty.

You can use rmdir /s if there are things in it:

C:>rmdir f  
The directory is not empty.  

C:>rmdir /s f  
f, Are you sure (Y/N)? y  


And you can do all that without the command prompt.

Recursion is a concept in programming. I don’t think you mean that. And whether something is implemented using recursion or not, is irrelevant to you since no doubt you don’t intend to limit yourself.. and you weren’t just intellectually curious about implementations.

And if you are having trouble deleting a directory, then think for example, about what error you get when you try to delete the directory you are struggling to delete. You could always boot off a live CD and delete it.

for /f "delims=" %i in ('dir /ad /s /b') do @rd "%i"

  — bobbymcr

@bobbymcr ‘s answer works, however it would need to be run (n-1) times the maximum directory depth to be certain that there are no empty directories. This is because for will feed rd directories in the descending order dir lists them, attempting remove the parent directory before it has relieved it of its children.

Since rd will only remove directories that are already empty sort the children first and empty nests will be removed from a tree in a single pass.

Make a [meow].bat file, and run it from the directory you want to prune. The first paramiter is an alternative tempfile name to use if needed.

:: Asserts the default tempfile name if none is specified.
IF "%1"=="" (
set __tempfile=meow.tmp
) ELSE (
set __tempfile=%1
echo %__tempfile%

:: Checks to see that it won't destroy an existing file
IF NOT EXIST %__tempfile% (
:: Gets list of directories in descending depth.
dir /ad /b /s > %__tempfile%
:: Removes empty directories specified in the list sorted in reverse order
for /f "delims=" %%i in ('sort /R %__tempfile%') do @rd "%%i"
:: Deletes tempfile
del %__tempfile%
) ELSE (
:: if the tempfile already exists.
@ECHO Tempfile already exists, please delete %__tempfile% and try again.

Leave a Reply

Your email address will not be published.