As an Amazon Associate, I earn from qualifying purchases.
  1. Home
  2. How To

Delete Empty Folders Recursively Using CMD

Staff // (Updated: )

When it comes to automating IT-related tasks, batch scripting is the best choice. And if you are using Windows you have really good alternatives like python, cmd (dos) and Powershell. In this post, I will show you how to delete empty folders and subfolders using CMD.

CMD is the new slang for Dos lately. Because it is launched using cmd command on Windows Run box. Using cmd we can automate many repetitive tasks easily.

Script to Delete Empty Folders with Subfolders

If you want to delete empty folders along with all subfolders recursively, copy the following script and save it as deleteblank.bat into the folder you want to clean up.

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

After you have saved the file, simply run this script by double-clicking on the batch file. You need admin rights to run this kind of scripts on your machine. If you have enough rights, the script will run and delete all empty folders under that directory.

If you are into Scripting, you may like other scripts I have shared under Scripting category. I also have a few Python and Powershell scripts. If you like this script or need any assistance, please share your comments in the discussion link below.


  • Bob

    Windows 10 – doesn't work.
    Generates error: %%i was unexpected at this time.

    • Metin Şaylan

      This code needs to be in a batch file. If you want to run it directly, remove duplicate %s.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More How To
Share Share Tweet