I have a directory on my Windows 7 machine that has hundreds if not thousands of sub-directories. Some of them have files, some do not. I want to delete all the empty directories.

Looking at the del and rmdir DOS command, it does not look like you can recursively do this without deleting all the files. Is there a way to do this from the command line?

  • I am amazed there's no simple answer to this question. – billpg Apr 5 '12 at 12:45
  • @billpg: simple as it could get: xcopy FROMDIR TODIR /s. See this SO answer: stackoverflow.com/a/14742810 – eckes Mar 20 '13 at 7:42
  • 1
    XCOPY deletes directories? – billpg Mar 20 '13 at 11:24
  • 1
    It can be done easily using ROBOCOPY. See my answer below for details. – Varun Sharma Oct 16 '15 at 18:48

11 Answers 11


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.

  • 9
    P.S I suggest you try the GUI-based tool first, before trying any command-line commands that can potentially delete all files. – caliban Sep 11 '09 at 13:50
  • 1
    Make sure it is truly empty. A hidden desktop.ini is still considered a file. same as a thumbs.db. If you want to verify whether this works, create a test directory with directories inside that are empty and populated. It works, I've verified. – caliban Sep 11 '09 at 18:19
  • 1
    Yep. It works. My mistake. – EBGreen Sep 11 '09 at 18:27
  • 9
    for /f %d in ('dir /s/b') do rmdir "%d" should work as rmdir cannot remove a non-empty folder – seanyboy Sep 16 '09 at 13:14
  • 3
    Just a warning, this script destroys directory junctions too. I learned this the hard way. – Pyroglyph May 22 '16 at 15:56

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.

  • 3
    Magic. So this moves all non-empty folders to the very same location they were already in (so does nothing?), and skips empty folders? But then: why would skipping them result in deletion? And wouldn't this move nested folders into folder1? Scary, unless tested well. – Arjan Sep 13 '15 at 11:15
  • 4
    @Arjan I have tested it, and it works perfectly!. To understand its working, its like moving all files to different location, except empty folders, and then deleting the left-behind empty-folders. /MOVE copies the files first, and then deletes the source file after copying. So, as /S is used, it copies the non-empty folders to destination(same folder in the above case). Then, it deletes the empty folders(like, its thinking that empty-folders has already been copied). No, it does not move nested folders into folder1, as ROBOCOPY moves folders recursively. – Varun Sharma Sep 13 '15 at 19:56
  • 4
    it even works with UNC paths (at least on win7) ! Brilliant ! – Arioch 'The Oct 15 '15 at 14:11
  • 3
    This is a very elegant solution without any third-party software needed. Thank you! – Marcus Mangelsdorf Dec 28 '17 at 20:20
  • 2
    This just did its magic and worked perfectly. Thanks! – CharleyDC5 Apr 23 '19 at 14:36

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 {} +
  • This can also now be done using Windows 10 Bash. – March Ho Oct 7 '17 at 19:06
  • 1
    For my install of Windows 10 this (find . -type d -empty -delete) fails from the command prompt with the following error: FIND: Parameter format not correct – ImpressTheNet Web Developer Jan 4 '18 at 22:39
  • It works in Git Bash also – caot Sep 4 '20 at 15:54

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

  • 1
    The one-liner batch file didn't work for me (I get the error "%%d was unexpected at this time."), and the Remove Empty Directories program will not install (it says on the linked page that it is not compatible with Windows 7 anyway). This program worked like a charm. – Phoenix Mar 7 '10 at 11:52
  • 1
    As user36580 wrote below, "it is likely you are running directly from the command line. In that case, change the double %% to a single %" – Lachlan McD. Jul 15 '12 at 3:27
  • 1
    I like this over the pure command-line version as it allows you to preview the things that get deleted. You can always call it from the command-line using "%~dp0\EmptyFolderNuker.exe" %cd%. – Jeroen Wiert Pluimers Dec 27 '14 at 14:30
  • 1
    @rob-kam The URL for "EmptyFolderNuker" isn't available anymore. – PeterCo Aug 9 '19 at 8:13
  • @PeterCo Thanks, fixed now. – Rob Kam Aug 12 '19 at 19:49

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

  • Thanks. Found that this only applies to commandline not batchfiles. According to superuser.com/questions/444474/… by Dennis, if we want to use this in a batchfile it should be for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i". The for behaviour is slightly different between commandline and batchfiles. – Brian Mar 10 at 22:21

If you have Cygwin installed, you could do this:

find -type d -exec rmdir {} \;
  • 4
    Not necessarily. That might not delete directories with only empty subdirectories. You might have to reverse it. find -type d -print0 | tac | xargs -0 rmdir – Ryan C. Thompson Sep 11 '09 at 22:56
  • I never knew about "tac" before. That's really nifty! – Anthony Giorgio Sep 12 '09 at 1:52
  • 1
    Or use find (starting-directory) -depth -type d ....  The -depth option tells find to do something like reverse the order of the output — it goes to the deepest directory levels first, then works its way back up. – G-Man Says 'Reinstate Monica' Jun 21 '15 at 3:35

If you're working in emacs (making this platform-agnostic), the following works:

(defun *-delete-empty-directories (root-directory)
  "Recursively delete empty directories in ROOT-DIRECTORY.

When called from dired, `dired-current-directory' is used for

  ;; Interface
  (interactive (list (if (eq major-mode 'dired-mode)
                         (expand-file-name (dired-current-directory))
                       (read-from-minibuffer "Root directory: "))))
  (when (or (null root-directory) (string= "" root-directory))
    (user-error "No root directory provided"))
  (when (called-interactively-p 'interactive)
    (unless (yes-or-no-p (format "Delete all non-empty directories in `%s'? "
      (user-error "Directory `%s' has been left untouched" root-directory)))

  ;; Implementation
  (require 'f)
  (let ((entries (f-directories root-directory)))
    (while entries
      (let ((curdir (car entries)))
        (when (f-directories curdir)
          (*-delete-empty-directories curdir))
        (unless (f-entries curdir)
          (delete-directory curdir)
          (message "Directory deleted: `%s'" curdir))
        (setq entries (cdr entries)))))
  • ...Please tell me you wouldn't have to type all of this in order to accomplish this in emacs. – Hashim Aziz Dec 31 '16 at 0:56
  • 1
    @Hashim The idea is that you have it defined in your init and then bound to a key. Technically I could have it bound to D if I wanted to, but since I don't have a common need for this, all I have to type is M-x *ded RET and bam–done. (If I did indeed have to type this every time, you're absolutely right that it'd be absurd – luckily that's not the case.) – Sean Allred Jan 1 '17 at 7:33
  • Fair enough, that sounds more plausible. – Hashim Aziz Jan 1 '17 at 22:30
  • Does M-x *ded RET have any significance or is it random? If the latter, how do you keep track of all the shortcuts you've assigned in emacs? Does memorisation suffice or do you find yourself having to look them up? – Hashim Aziz Jan 1 '17 at 22:32
  • 1
    @Hashim *ded is an abbreviation for the full function name *-delete-empty-directories. Many completion engines (such as ivy, the one I use) understand this kind of fuzzy matching. As for keeping them memorized… I simply don't have to. The functions I've defined all start with * so I can easily find them if they're not bound to a key. Otherwise, emacs itself will tell me if there was a faster way of calling the function. If all else fails, there's always apropos. – Sean Allred Jan 2 '17 at 2:10

Combining Gareth's and G-Man's posts:

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

Edit: But that gave a security error because of 'C' in my PATH var...so instead:

$find . -depth -type d -empty | while read dir; do (rmdir -v $dir); done

I don't use xargs because it appears to have an input line limit (of about 1024 lines, I think?), whereas

 while read x; do (command $x); done

just keeps on going for as long as it has input. Leave out the '-v' verbose flag if you don't want to see the results and/or want it to run faster.


None of the previous answers worked for me, so I made the following file:



@setlocal enableextensions enabledelayedexpansion

for /D %%d in (*) do (
cd %%d
@CALL :walk_tree %%d
cd ..
rd %%d


Usage: Cd to the top level directory you want to clean up. From the command line prompt, run:


Warnings will show up for non-empty directories.

Usual disclaimers: Use at your risk. Backup before testing. etc.


The 4NT shell (nowadays Take Command) has a /sx option to "DEL". /S is recursive, the appended X is remove empty dirs.


You can use Powershell:

Get-ChildItem "Path" -recurse | 
  Where-Object {($_.PSIsContainer -eq $true) -and ((gci $_.fullName).count -eq 0)} | 
    Remove-Item -Force

Not the answer you're looking for? Browse other questions tagged or ask your own question.