PowerShell Delete Empty Folders: Safe Recursive Steps

You can use PowerShell to find empty folders and remove them, including nested folders. Preview the matches first, then run the cleanup only on the intended path.

Photo illustrating PowerShell Delete Empty Folders: Safe Recursive Steps
In this article

PowerShell can find empty folders beneath a chosen parent path, making it useful for cleaning up a directory tree without checking each folder by hand. Preview the matching folders before you remove anything so you can review what the cleanup would affect. [1] You may also find this useful: How to Find Empty Folders in Windows 11 Safely.

For nested folders, process candidates deepest-first: removing an empty child can leave its parent empty too. [1] This article walks you through finding and previewing empty folders, removing them in the right order, and checking what remains afterward. [1]

Choose the folder you want to clean

Choose one specific parent folder for the cleanup, such as C:\Temp\ProjectFiles, rather than pointing PowerShell at an entire drive or a broad location. A narrow path helps keep the intended cleanup area clear as you work through the process.

The cleanup is for empty directories; it is not meant to delete files from folders that contain them. The example PowerShell script checks each directory for children and filters out directories that are not empty before removal. [1]

Decide whether hidden folders should be included before running the cleanup. The documented -Force switch includes hidden folders in the recursive directory search, so leave it out if hidden folders should not be part of the cleanup. [1]

For example, if you intend to clean a project folder, choose that folder as the parent path instead of selecting C:\ or your user profile. Then the cleanup is scoped to the directory tree beneath the parent you chose, rather than a much broader area.

Photo illustrating Choose the folder you want to clean

Preview empty folders before deleting them

  1. To preview empty folders, run this PowerShell pipeline with your parent folder in place of C:\Temp:

    $rootFolder = 'C:\Temp'
    Get-ChildItem $rootFolder -Recurse -Directory -Force |
        Where-Object { (Get-ChildItem $_.FullName -Force | Select-Object -First 1).Count -eq 0 }
  2. Replace C:\Temp with the full path of the folder you want to inspect, such as C:\Users\Alex\Documents\ProjectFiles. The first line stores that path in $rootFolder, and Get-ChildItem searches it recursively for directories. [1]

  3. In the filter, Where-Object keeps only directories whose child listing has a count of zero. The inner Get-ChildItem checks a candidate directory for children, while Select-Object -First 1 limits that check to the first result. [1]

  4. -Directory restricts the recursive search to directories, and -Force includes hidden folders in the search and child checks. The pipeline displays matching paths; it does not include a removal command. [1]

  5. Read every displayed path before moving on. For example, if the results include a project subfolder you want to keep, stop rather than using those matches for cleanup. [1]

A folder-cleanup app lists selected folders and displays a confirmation dialog asking whether to delete them.

Delete empty folders from the deepest level up

  1. Set $rootFolder to the parent directory you intend to clean, then run the recursive pipeline below to remove empty folders from the deepest level upward. The example uses C:\Temp; replace that path with your chosen target. [1]

    $rootFolder = 'C:\Temp'
    Get-ChildItem $rootFolder -Recurse -Directory -Force |
        Sort-Object -Property FullName -Descending |
        Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } |
        Remove-Item
    
  2. Before running it, confirm that $rootFolder points to the intended location and preview which paths match the filter. You can do this by temporarily replacing Remove-Item at the end with Select-Object -ExpandProperty FullName; review the resulting paths before restoring Remove-Item. [1]

    Removing folders changes the directory structure and may be difficult to undo, so do not run Remove-Item until you have reviewed the preview. [2]

  3. Keep Sort-Object -Property FullName -Descending before Where-Object and Remove-Item. This order processes deeper paths first, so if a child folder was the parent’s only item, removing the child leaves the parent empty in time for the filter to select it too. [1]

The pipeline first finds directories under $rootFolder, including hidden folders because it uses -Force. It sorts their full paths in descending order, then checks each folder for a child; the filter passes folders with no child to Remove-Item. [1]

Check what remains after cleanup

After cleanup, preview the same parent path again to see which eligible empty folders remain. [1] Use the identical path and preview pipeline you used before; for example, if you checked C:\Temp, run that preview again against C:\Temp. [1]

A parent directory may appear in the follow-up preview only after its child directories have been removed. [1] The cleanup processes directories deepest-first, so a parent that contained only an empty child can become empty in turn. [1]

If the preview still lists folders, verify that the target path is the one you intended and is spelled correctly. [1] Then check whether hidden children are part of the result: -Force includes hidden folders, so a hidden child can affect whether its parent counts as empty. [1] For example, a visible folder may look empty in File Explorer while containing a hidden subfolder that the preview includes. [1]

If a folder is not removed

If a folder is not removed, first check that the cleanup command points to the intended parent folder and that its path is spelled and formatted correctly. For example, a command aimed at C:\Temp\Projects will not clean a similarly named folder elsewhere, so compare the path in the command with the folder you mean to target.

The -Force switch includes hidden folders in the cleanup pipeline, so use it only when you intend hidden folders to be included. A hidden child folder can affect whether its parent appears empty; if you want to exclude hidden folders, remove -Force from the relevant parts of the pipeline. [1]

If the path looks right but the command still does not behave as expected, consult the Remove-Item documentation for the command’s behavior and available switches rather than guessing at extra options. The documentation lists -Force among the supported switches. [2]

Run the cleanup only after reviewing the matches

Keep the cleanup narrow: choose a specific parent folder, preview the empty directories beneath it, and remove matches deepest-first. That order lets PowerShell remove an empty child before checking whether its parent is empty. [1]

Review the preview paths before you run the removal step. If it includes a folder you want to keep, stop and narrow the target or adjust your plan rather than proceeding. For example, if the preview shows a project folder you still use, leave it out of the cleanup. [1]

Sources

  1. Remove empty folders using PowerShell – meziantou.net

  2. Remove-Item (Microsoft.PowerShell.Management)