Stand by...

 Print This Post

Recursively delete Subdirectories from a command prompt

Some might have noticed that the “del” command will only remove files in a directory and it’s subdirectories.
If you want to delete Subdirectories from a given starting directory (without deleting the root directory itself) you might want to parse through the root folder and delete every directory that is found.

Here’s a small for loop which does the trick.

In this example, all folders within the %temp% folder that start with “OL” would be deleted (Since that is what the “dir” command would return). You might want to modify the path and the wild card to suit your requirements.

In a command line:
for /f %i in ('dir %temp%\OL* /B /D') do rd %i /Q /S
In a batch file:
for /f %%i in ('dir %temp%\OL* /B /D') do rd %%i /Q /S

This post was written by microtom and has 36,690 views in total

(4 votes, average: 4.25 out of 5)
Loading ... Loading ...

7 Comment(s). Add a comment or Trackback

  • Kawu  13:37 Jul 12, 2014 

    This was utterly helpful, thanks a lot!

  • microtom  09:41 Oct 07, 2013 

    Hello Arasu

    try this:
    for /f %i in (‘dir D:\SOP_LOCAL_DATA\Data\* /B /D’) do rd %i /Q /S


  • arasu  09:27 Oct 07, 2013 

    Server: Windows2008
    When i use “for /d %i in (%D:\SOP_LOCAL_DATA\Data%\*) do rd “%i” /Q /S” its not deleting any subfolder/files from the directory.

    Could you please give me the exact command? i would to delete all the subfolders/files from the directory “D:\SOP_LOCAL_DATA\Data”.


  • Pedro Santos  22:23 Oct 01, 2010 

    forgot that very long file names when making the rd function, give an error – invalid file name.

  • Pedro Santos  22:21 Oct 01, 2010 

    echo off
    dir youdirectoryhere /og/s/b/d >txtfile.txt
    for /f “usebackq delims==” %%i in(txtfile.txt) do rd “%%i” /q
    delete txtfile.txt

    I made a file by using this commands,
    first it turns off anymessage
    after that finds all directories and subdirectories and store in a txt file
    after that search the directories stored in the txt and try remove each one
    after that remove the txt file

    the solution right above couldnt find subdirectories inside subdirectories -> recursive

    the problem with this algorithm that i made is that it needs to be run at least twice to remove most of the recursiveness.

    Pedro santos

  • microtom  10:44 Feb 15, 2010 

    Thanks LJ! I always start with “for /f” and forget about the /d switch. You’re absolutely right.

  • LJ  00:27 Feb 15, 2010 

    If you want to delete ALL subdirectories regardless of their name, I found it better to use the directory switch. The above script couldn’t handle some of the longer name directories I wanted removed.

    So this is what I would use to delete *ALL* subdirectories in %temp% (notice I use “” around the output as a precaution to catch long folder names that may contain spaces and strange characters)

    In a command line:
    for /d %i in (%temp%\*) do rd “%i” /Q /S
    In a batch file:
    for /d %%i in (%temp%\*) do rd “%%i” /Q /S

    If you want to be more specific about the folders you are deleting you can do the same as the original script and delete the folders you want to filter starting with whatever letter or word you choose.

    E.g. — Where “OL” is the starting letters of the directories you wish to filter out.
    In a command line:
    for /d %i in (%temp%\OL*) do rd “%i” /Q /S
    In a batch file:
    for /d %%i in (%temp%\OL*) do rd “%%i” /Q /S

Leave a comment ↓

Welcome back, (change)