Recursively delete Subdirectories from a command prompt

Home  >>  Script Repository  >>  Batch  >>  Recursively delete Subdirectories from a command prompt

Recursively delete Subdirectories from a command prompt

On November 4, 2009, Posted by , In Batch,Script Repository, By , , With 7 Comments

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

7 Comments so far:

  1. LJ says:

    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

  2. microtom says:

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

  3. Pedro Santos says:

    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

  4. Pedro Santos says:

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

  5. arasu says:

    Hello,
    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”.

    Regards,
    Arasu

  6. Kawu says:

    This was utterly helpful, thanks a lot!

Leave a Reply