Over time, hundreds of empty folders and junk files may take up your hard disk. While the junk files occupy disk space and can be cleaned up using Disk Cleanup or Storage settings, the empty directories remain.
The empty folders don’t take up disk space, but to organize your data better, you may want to prune them every once in a while.
This article discusses different methods to find and remove empty folders on your computer.
Find/Remove Empty Directories in Windows
How to Find and Remove Empty Folders in Windows?
An empty folder or directory is one that has no file or sub-directory in it. Empty folders don’t take any disk space, but too many of them can be a nuisance. To find and remove them, use one of the methods below.
Important: Running an automated tool or batch file to delete empty folders in the entire system drive recursively is usually not recommended. This is because some programs may fail to work and throw a bizarre error when it finds an essential folder missing. As always, the standard warning applies. Be sure to have appropriate backups, preferably on an external drive, before manipulating the file system or the Windows registry.
RELATED: Find and delete 0-byte files recursively in a folder in Windows
Let’s start with a neat GUI tool, followed by native command-line and scripting methods.
1. Remove empty folders using the “Find Empty Files-n-Folders” utility
Find Empty Files-n-Folders is an excellent tool that can find and delete empty files (0-byte) and empty folders recursively under a folder tree.
- Download Find Empty Files-n-Folders (600KB installer) from Ashisoft.com.
- Select the folder and click Scan Now.
The tool will list empty files and folders in separate tabs. - Click Mark all Folders and then click Delete Folders.
Similarly, if you want to delete the 0-byte files in the selected folder tree, click on the Empty Files tab.
Ashisoft.com has other awesome tools that you can check out!
https://www.4dots-software.com/emptyfoldercleaner/
2. Remove empty folders using “for” and “rd” commands
You can use the Command Prompt internal commands for
and rd
to enumerate the list of folders and delete the empty ones.
- Press & hold the Shift key, right-click on a folder and click Open command window here. This opens Command Prompt at the current folder location, which can also verify in the console.
Note: You must make sure that you run the following command from the exact parent folder path where you need to find and remove empty sub-folders.
- Once you’re in the desired directory in the Command Prompt, run this command-line:
for /f "delims=" %i in ('dir /s /b /ad ^| sort /r') do rd "%i" 2>NUL (or) for /f "usebackq delims=" %i in (`"dir /s /b /ad | sort /r"`) do rd "%i" 2>NUL
This deletes all the empty directories across sub-directories from the base folder path, including nested empty folders.
If you’re going to use the above command in a batch file, then replace
%i
with%%i
.
Important: The above command won’t delete folders with foreign characters — e.g., 蜍穂. You may want to check out DelEmpty.exe (covered in this article) or other methods to delete folders with Unicode characters.
The above command-line is courtesy of Raymond Chen of Microsoft, via his blog The Old New Thing. In this post, Raymond’s command-line has been slightly modified so that it also deletes folder names containing space(s).
How does the command work
The above command lists all the sub-directories (recursively) in the current base path and sorts the list (sort /r
) in reverse order. The reverse order sort is to make sure that the enumeration is done bottom-up. This is important for deleting empty directories, as you have to remove the subdirectories before removing the parent.
Then it attempts to remove the list of directories (in reverse sort order) using the RD
command. If a directory is not empty, it proceeds on to the next directory and so on. The 2>NUL
command ensures that the “directory not empty” output text is suppressed.
The only disadvantage of the above method is that it’s not helpful if you want to find empty folders without deleting them. If you want to find the list of empty folders, use the command-line in the paragraph below, or follow any other method described in this article.
Find empty folders, but don’t delete
To only find the empty folders without deleting them, use this command-line syntax:
for /r "D:\uploads\2019" /d %F in (.) do @dir /b "%F" | findstr "^" >nul || echo %~fF
It shows you the output containing the list of empty directories.
Let’s say you have empty nested folders like this:
C:\1 └───2 └───3 └───4
Running the above command lists only the deep most empty folder → 4
. Technically, it’s correct because a folder can’t be considered empty if a sub-folder or a file exists in it.
3. Find and remove empty folders using Robocopy
Robocopy is a powerful built-in file copy tool that has a lot of advanced features. We’ve covered Robocopy in the article Compare the Contents of Two Folders in Windows.
Let’s assume you have a folder named cars
, which contains several sub-folders, of which some are empty. We’re going to delete the empty ones using Robocopy.
The trick here is to use the Robocopy move command, passing the exact same folder path for both “source” as well as “destination.”
- Open a Command Prompt window.
- Type the following command and press ENTER:
robocopy "d:\automobile rates\cars" "d:\automobile rates\cars" /S /move
The most important thing to note here is that the source and destination paths are (deliberately) the same. The
/S
parameter instructs Robocopy not to move empty folders to the “destination” path. As we’ve mentioned the same paths for source and destination, Robocopy will delete the empty folders due to the/S
switch.
The empty folders in the chosen path are now cleared.
Want to find empty folders but not delete them?
You can use the /L
(list-only) command-line argument with Robocopy so that it only carries out a dry-run instead of performing the actual copy/move operation.
/L
:: List only – don’t copy, timestamp, or delete any files.
/MOVE
:: MOVE files AND dirs (delete from source after copying).
/S
:: copy Subdirectories, but not empty ones.
You can see the number of files in a column hear the folder path. The 0
s mean that those folders have no files.
Let’s put this command to a real test!
My %APPDATA%
folder is full of empty folders added by obsolete programs.
Before the cleanup, the Appdata\Roaming folder had 681 folders, as seen in the folder properties.
I opened the Command Prompt and ran the following command:
robocopy "%appdata%" "%appdata%" /S /MOVE
Note: %appdata% environment variable translates to C:\Users\{username}\AppData\Roaming
folder. You can use the full folder path or the equivalent environment variable with Robocopy. Either is fine.
It has successfully removed 94 empty folders in my %APPDATA%
folder and sub-folders.
Editor’s note: In the command-line output below the statistics section, the following error appeared:
The process cannot access the file because it is being used by another process.”
Because we’ve instructed Robocopy to move (/MOVE
) the %APPDATA% directory, the tool was trying to clean up the “source” folder after “moving” it to the destination. It couldn’t, as %APPDATA% is a special folder. The error doesn’t occur when used a directory path that’s not a special folder. As everything went on fine, I simply ignored the (trivial) error.
4. Find and Remove empty folders using PowerShell
The following PowerShell command-line deletes empty folders located under the specified base folder recursively.
- Start PowerShell and type the following command:
(gci "folderpath" -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | remove-item
Replace “folderpath” with the base folder location. For example, I’m specifying the Roaming folder here:
(gci "C:\Users\ramesh\AppData\Roaming" -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | remove-item
This deletes all the empty sub-folders under my
%appdata%
folder recursively and doesn’t show any output unless it encounters any error(s).
Note that the above PowerShell command clears only the last level of the empty nested folder. For example, let’s say you have empty nested folders like this:
C:\1 └───2 └───3 └───4
Running the above command clears the deep most empty folder → 4
. Rerunning the script will clear another level of a nested folder (3
), and so forth.
You can use the PowerShell script at Svendsen Tech PowerShell Wiki to work with nested folders.
Alternately, you can use the next method, a Windows Scripting solution, to clear all empty folders, including nested ones, recursively.
Find empty folders, but don’t want to delete them?
Want to get the list of empty folders, but don’t want to delete them? Use this command-line example instead:
(gci "C:\Users\ramesh\AppData\Roaming" -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName | Out-GridView
The command outputs the list of empty folders with full paths to a grid view control.
Tip: In the grid view, you can select all and copy the selection by pressing Ctrl + C
It’s a good idea to preview the list before running the command to delete the folders.
5. Find and Remove empty folders using Windows Scripting
Microsoft employee Jeremy Jameson wrote a VBScript that deletes empty folders recursively. I’ve added more lines in the script to force delete empty read-only directories, output the aggregated list of empty folders. It also outputs the list of empty folders that could not be deleted, along with the corresponding error description.
The script is capable of deleting nested empty directories across sub-folders.
'Deletes empty folders recursively in Windows
'https://www.winhelponline.com/blog/find-and-delete-empty-folders-windows/
Option Explicit
If (WScript.Arguments.Count <> 1) Then
WScript.Echo("Usage: cscript DeleteEmptyFolders.vbs {path}")
WScript.Quit(1)
End If
Dim strPath : strPath = WScript.Arguments(0)
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim objFolder : Set objFolder = fso.GetFolder(strPath)
Dim sDelList, sDelErr, sFolderPath
Dim iCnt
iCnt = 0
DeleteEmptyFolders objFolder
Sub DeleteEmptyFolders(folder)
Dim subfolder
On Error Resume Next
'Skip errors when accessing Junctions etc.
For Each subfolder In folder.SubFolders
DeleteEmptyFolders subfolder
Next
On Error Goto 0
If folder.SubFolders.Count = 0 And folder.Files.Count = 0 Then
sFolderPath = folder.Path
On Error Resume Next
fso.DeleteFolder folder.Path, True
If Err.number <> 0 Then
sDelErr = sDelErr & Err.number & ": " & Err.description & _
vbCrLf & sFolderPath & vbCrLf & vbCrLf
Else
sDelList = sDelList & vbCrLf & sFolderPath
iCnt = iCnt + 1
End If
On Error Goto 0
End If
End Sub
If sDelList = "" And sDelErr = "" Then
WScript.Echo "No Empty folders found under the " & _
"""" & strPath & """" & " tree"
WScript.Quit
End If
If sDelList <> "" then sDelList = "List of empty folders deleted" & vbCrLf _
& String(38,"-") & vbCrLf & sDelList & vbCrLf & _
vbCrLf & "Total: " & iCnt & " folders deleted."
If sDelErr <> "" then sDelErr = "These folders could not be deleted" & _
vbCrLf & String(45,"-") & vbCrLf & sDelErr
WScript.Echo sDelList & vbCrLf & vbCrLf & sDelErr
- Download del_empty_folders.vbs
Optionally, you may rename the script file accordingly, let’s say delempty.vbs, and move it to the C:\Windows
folder.
You can run the script using two ways:
via Command Prompt, by running:
cscript.exe delempty.vbs "folder_path"
via GUI, by running:
wscript.exe delempty.vbs "folder_path"
via the Send To menu
You can create a shortcut to the script in your SendTo folder and name it Delete Empty Folders.
Then, right-click on a folder where you want to delete empty sub-folders recursively → click Send To → click Delete Empty Folders in the Send To menu.
You’ll see the list of empty folders deleted and the total, and folders that couldn’t be deleted with the respective error codes displayed.
6. Find and Remove empty folders using DelEmpty.exe
DelEmpty.exe is a console tool from IntelliAdmin that can delete empty directories recursively. This program can also swiftly delete the empty 0-byte files recursively.
The following is the command-line syntax for the program:
DelEmpty.exe OPTIONS [PATH]
Argument | Description |
-f | Delete empty (0-byte) files |
-d | Delete empty directories |
-v | Verbose mode |
-c | Confirm mode (Shows what was deleted) |
-s | Include sub-directories (traverse subfolders) |
-l | List what would be deleted (will not delete) |
-y | Delete without (y/n) prompt |
Example 1: To list the empty folders under the Mozilla directory under AppData, I used the following command-line:
DelEmpty.exe "%AppData%\Mozilla" -d -c -s -y -l
The above command shows the list of empty folders, but will not delete them since the -l
(list only) switch is used.
For folder names containing space(s) — e.g., Mozilla Firefox
, be sure to include the double-quotes around the path.
Example 2: To delete the empty folders under the Mozilla directory
under %AppData%
, I run the same command-line but without the -l
switch:
DelEmpty.exe "%AppData%\Mozilla" -d -c -s -y
The program traverses through every subfolder and deletes the empty folders. Nested empty folders are removed too. For example, if you were looking to remove empty directories from this structure:
C:\Folder1\Folder2\EmptyFolder1 C:\Folder1\Folder2\EmptyFolder2
After the EmptyFolder1
and EmptyFolder2
folders are removed, Folder 2 is empty as well — and it will also be removed. Also, DelEmpty.exe
is capable of removing folders with unicode/foreign characters as well — e.g., 蜍穂
7. Find and Remove empty folders Using SearchMyFiles utility
SearchMyFiles is a utility from Nirsoft.net that’s a good alternative to the standard Windows Search feature. SearchMyFiles can be used to quickly find and/or delete empty directories in Windows.
- In the Search Options dialog of the SearchMyFiles utility, select Summary Mode.
(The ‘Summary Mode’ feature displays a general summary of all scanned folders, instead of the files list. For every folder, the following summary information is displayed: Total size of all files, total files count, and other information.)
- Select “Only folders with zero files and subfolders” in the drop-down list box.
- Type the base folder path(s) to find the empty folders. Add a comma as a separator if you plan to search more than one base folder.
- Once done, click the Start Search button.
- You’ll find the list of empty folders under the specified base folder. If you wish to delete them, select the items in the listing, right-click and select Delete Selected Empty Folders. That’s it. It deletes the (selected) empty folders from your computer.
If you know of any other methods to remove empty folders in Windows, let’s know that in the comments section below.
Last reviewed on 05-July-2023.
One small request: If you liked this post, please share this?
One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:- Pin it!
- Share it to your favorite blog + Facebook, Reddit
- Tweet it!
Your “Remove empty folders using “for” and “rd” commands” code is broken. It doesn’t work.
The original code by Raymond Chen works fine though.
@Slava: If you’re going to use the above command in a batch file, replace
%i
with%%i
. It should run.5. Find and Remove empty folders using Windows Scripting
Option Explicit
If (WScript.Arguments.count = 0) Then
WScript.Echo(“Usage: cscript DeleteEmptyFolders.vbs {path}”)
WScript.Quit(1)
End If
…
…
****Fixed****
@Eiríkur: WordPress seems to have stripped off the special characters in that code. I’ve now fixed the formatting. Thanks for your comment.
Good Article ..CMD option helped
Thank you for the thorough synopsis of 5 different methods.
Method 2, using “for” and “rd” commands, seems not to work with Unicode; it overlooked a folder named “✇”.