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

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.

  • 6
    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
  • 8
    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
  • 1
    Look at stackoverflow.com/a/34025977/694360 to find out how to add this functionality to context menu. – mmj Dec 1 '15 at 17:36

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.

  • 1
    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
  • 2
    @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
  • 2
    it even works with UNC paths (at least on win7) ! Brilliant ! – Arioch 'The Oct 15 '15 at 14:11
  • Can Robocopy move files to the recycle bin? – user598527 Oct 13 '17 at 16:37
  • This is a very elegant solution without any third-party software needed. Thank you! – Marcus Mangelsdorf Dec 28 '17 at 20:20

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
  • 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

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

The excellent Glary Utilities has this and a bunch of other great features.


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


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

protected by nhinkle Jun 19 '11 at 22:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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