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

  • The 'useless files' criterion complicates the problem. If you know in advance that only unwanted files are in the directory tree, you can force a recursive delete. But only you can define what constitutes 'useless' so for this action to be generalised or automated, it would be safer to do it in two steps: first recursively delete all files with known unwanted names, then delete the directories (without force, in case there are wanted files in there). This becomes nontrivial and I'd use a script or Cygwin if I want that level of control.
    – idoimaging
    Feb 26 '13 at 18:32

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.

  • 1
    I had to run this a few times. I think the recursion is top down.
    – jozxyqk
    Mar 23 '14 at 9:04

Try this one: Remove Empty Directories - CNET Download.com

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.

  • Looks like Moab and probably velio figured out what you want to do. Scan through your hard drive searching for empty directories, or directories with little junk windows files, so you can delete them. I guess that's one way of saving space ;-)
    – barlop
    Dec 27 '10 at 7:55
  • Actually on some operating systems term "recursively" is used for directory deletion. For example famous rm -rf.
    – AndrejaKo
    Dec 27 '10 at 8:19
  • @AndrejaKo I know about rm -rf and recursive.. and I wrote with that in mind too, and I am pretty sure that in the rm case the implementation is recursive.
    – barlop
    Jan 3 '11 at 19:36
  • I see velio and moab figured out what you want to do(it wasn't what I thought). Really you could have asked your question without redefining the word empty. It already has a meaning which is obvious and shared with english and even rmdir uses the term. Your question really needs a rewrite
    – barlop
    Dec 4 '12 at 19:42

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.

Belvedere can do that, but you'll need to be more specific than "useless" to be able to set up the right rules.


Use Command Prompt from Start/Run (or the combination WindowsR). Write cmd to start the console, then navigate to the drive the directories are on by inputting the drive letter (e.g. if they are on D:\ you put d:).

The path of the command line will change to the desired path. Then use the command cd to travel to the empty directories parent folder (cd D:\ParentDirectory\). You can use the Tab for Windows to put the entire path.

Once arrived at the parent directory use

del /F EmptyDirectory1

This method is the best one and also will delete folders or files that can't be deleted by Windows Explorer.

  • 3
    with an answer as yawn worthy as that, i'm a bit reluctant to correct you.. If he had trouble opening a command prompt, he could ask a question on that. But this is really for "super users", so you can assume they can open a command prompt, and if they can't they can ask. Otherwise there's no end to it. MOVE THE MOUSE UP, MOVE THE MOUSE DOWN. Onto your answer, DEL won't delete directories.
    – barlop
    Dec 27 '10 at 8:04