Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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? Or is there a tool that would do it for me?

share|improve this question
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: – eckes Mar 20 '13 at 7:42
XCOPY deletes directories? – billpg Mar 20 '13 at 11:24
It can be done easily using ROBOCOPY. See my answer below for details. – Varun Sharma Oct 16 at 18:48

7 Answers 7

up vote 57 down vote accepted

You can use this utility: Remove Empty Directories

Alternatively you can use this one-liner batch file:

for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"

(This works because rd will not remove a directory that contains files.)
One-liner taken from DownloadSquad, an excellent site to add to your RSS feeds. :)

share|improve this answer
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
that tool looks good. I will check it out and report back – mohlsen Sep 11 '09 at 14:31
+1 for recommending DownloadSquad. – alex Sep 11 '09 at 14:31
Using the batch version gives me an error: The system cannot find the file dir /ad/b/s | sort /R. – EBGreen Sep 11 '09 at 15:58
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

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

share|improve this answer
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
As user36580 wrote below, "it is likely you are running directly from the command line. In that case, change the double %% to a single %" – nness Jul 15 '12 at 3:27
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

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 {} +
share|improve this answer

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.

share|improve this answer
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 at 11:15
@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 at 19:56
it even works with UNC paths (at least on win7) ! Brilliant ! – Arioch 'The Oct 15 at 14:11

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

share|improve this answer

If you have Cygwin installed, you could do this:

find -type d -exec rmdir {} \;
share|improve this answer
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
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 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)))))
share|improve this answer

protected by nhinkle Jun 19 '11 at 22:58

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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.