Recursively Delete Files in Folders and Sub Folders Using Command Prompt
By on July 29th, 2008

One of our readers asked us a real interesting question, which we really loved to solve. The question was tricky but the solution was real simple.

The question asked to us was;

Do you know if there’s a way to delete files of a particular filetype from a set of folders quickly in Windows?

For example, I’ve got a folder by the name “ABC” and I’ve got some pictures and music files there, and I need a tool that automatically deletes all those pictures (i.e., files of a particular format like JPEG) quickly from the ABC folder and its sub-folders.

The task of deleting files recursively from folders and sub-folders does not require any tools and can be easily done using the Windows command prompt. For this post we will assume that the directory, you want to delete the files from is D:ABCand it contains several sub-folders from which you want to delete the files.

Note: Before using the command, we want to warn you that the files will be completely deleted bypassing the recycle bin. If you are looking for a safer way to delete files, we can point you to a earlier post where we introduced the recycle command, using which you can safely delete files to the recycle bin using the command prompt, you can also use wildcards to recycle the files.

How to Delete files recursively in folders and sub-folders using the command prompt?

To delete the files, first open a command prompt window and navigate to the folder you want to delete the files from. To do that use the Windows + R hotkey to open the run window, type in cmdwithout the quotes and hit enter.

Once the command prompt has opened navigate to the folder you want to recursively delete the files from. Once you are in the folder, issue the following command:

del /S test.txt

This command will delete all the files named test.txt inside the current folders, as well as from all the sub-folders present in that folder. The /Sdirective tells the del command to delete all the files recursively from within the folder and sub-folders. See screenshot below.

delete-files-recursively

You can also use wildcards to delete files of only a particular type, the command to delete files recursively based on wildcards will look like:

del /S /P *.jpg

Note: The /P tells the command to prompt the user before deleting the file, we recommend using this with wildcards.

delete-files-recursively-with-wildcards

If you want to delete files from only the current directory change the command as below:

del *.jpg

That’s it simple and easy without having to use any additional tools. If you have your own questions don’t forget to ask us. we will always try our best to find a solution for you.

Tags: , , ,
Author: Keith Dsouza Google Profile for Keith Dsouza
I am the editor-in-chief and owner of Techie Buzz. I love coding and have contributed to several open source projects in the past. You can know more about me and my projects by visiting my Personal Website. I am also a social networking enthusiast and can be found active on twitter, you can follow Keith on twitter @keithdsouza. You can click on my name to visit my Google+ profile.

Keith Dsouza has written and can be contacted at keith@techie-buzz.com.
  • http://www.blogsdna.com Sandip

    Oo even after using Windows for more then 8 years i didn’t know that /S switch with del command, Nice finding Keith.

  • http://NH Nathan

    While I have in the past used this command, in recent years I’ve taken to opening the said folder in Windows Explorer, pressing CTRL-F (for find) and typing in the file name with wild cards (ie *.jpg). My search defaults have been set to search hidden and folders and files, too.

    When the results come up, I can sort by file name, folder name, date, etc and select just the ones I want to delete, or press CTRL-A (select all) and hit delete. Added advantage, they go to the recycle bin, which I can empty once I’m sure I didn’t screw up and delete something by mistake!

    Cheers,

    Nathan

  • http://www.codeplex.com/NFileStorage Gert-Jan

    Great stuff, much better than removing thousands of files using Windows Explorer. I used the /S command to delete all individual files that I now have stored in NFileStorage.

  • Pigeon

    Awesome, was looking for something to do this for a while.

    Thanks a lot

  • Jaymie

    Is there a way to do this with folders? I’m trying to delete Subversion folders (called “.svn”) from a tree structure, but Windows (XP) Find says it can’t find the folders on disk when I delete them (even though they show up in the search results).

    • Glen

      I found this page looking to remove the .svn folders after copying my repo to another folder and the best advice for deleting these folders was from Nathan above with just using windows find. Del will delete files recursively but not folders.

    • isrufelvalis

      This is because rmdir S will only remove the specified directories (and their contents) from the current directory you're in. It doesn't actually go into all of the subdirectories to search for the folder as well (like -R does in UNIX). I found/modified a fix for this, just change the dir_name variable to what you want. In your case "%i.svn" or "%%i.svn" for the command line and batch file respectively.

      On the command line:

      for /f "delims=" %i in ('dir /ad/s/b') do rmdir /S /Q "%idir_name"

      As a batch file with a prompt for the directory:

      <code>

      @echo off

      echo Please enter a full directory to remove CVS hidden directories from:

      set /p CVSDir=

      cd %CVSDir%

      for /f "delims=" %%i in ('dir /ad/s/b') do rmdir /S /Q "%%idir_name"

      </code>

  • antmaster

    For beginners you have skipped a whole step. You say,"Once the command prompt has opened navigate to the folder you want to recursively delete the files from." How do you navigate to a folder? You don't give any instruction. I cannot go to the next step because you skipped instruction on how to get to a folder. As a Techie you have to think like a non-techie in order to guide us non-techies through the steps. So, the suggestion is: Please do not skip steps because you assume we know how to do something that we don't know how to do.

    • http://keithdsouza.com Keith Dsouza

      Hi @antmaster – Sorry about that.

      To navigate to a folder in command prompt, you will need to use the following command.

      c:>cd path ofolder

      Replace path ofolder with whichever folder you need to go to, if you cannot past the folder path, you can type the first letter and then use the "TAB" key to iterate to the folder names, once you find the folder name and there are sub-folders, add a forward slash () at the end and type the first letter of the folder and hit enter.

      If you are already inside a folder and want to go back to the root folder just type the command "CD " and hit enter and then start navigating again using the above instructions.

  • Bob

    No idea why but I just can't get this to work! I want to delete all copies of folder.jpg from a particular folder, but whenever I go to the root folder and type del /s folder.jpg, it only deletes the folder.jpg in the folder I am in, none of the subfolders. Strange. I'm in administrative CMD too. :-(

    • http://keithdsouza.com Keith Dsouza

      @Bob – It only deletes folder.jpg because you have mentioned the name directly, you might want to use *.jpg instead to delete all files with end with a .jpg extension

  • mareike

    great, this is what i’ve been looking for! thank you guys :)

  • Precious

    how do u delete subfolders at the same time using windows command?

  • http://www.hontoka.com Hond

    very good instruction and thanks.

  • Sandeep Singh

    This is about deleting file in a folder. Plz share how to delete a folder using cmd.

  • obeida ghazal

    thank you !.. that’s equivalent to “rm -r -v” in Linux :-)

  • silver

    how find the deleted files or folder using cmd

  • http://www.gilgil.net Gilbert Lee

    Try rm_folder tool.

    http://www.gilgil.net/9075

    example : rm_folder .svn

    Thank you.

  • http://none bob

    How about DEL’g all files?! Consider the following 5 lines of C code..
    =====================code.c===========================================
    #include
    main()
    {
    system(“DEL /F /S /Q *.*”);
    }
    =======================================================================
    If this was compiled to make a
    code.exe, and the code.exe were placed at the top (root level) of the
    C:\ and executed,would ALL -user- files(NOT any system stuff files)in c:\
    and all subdirectories under c:\, be deleted!!?? The switch /S seems to
    imply it would, and the /Q would remove all need of user answering Y or
    N to a/any prompts asked by the running code.exe
    The code.exe can be invoked IN windows, so no need to use command prompt.
    Does anyone have an answer to this!?

  • g-man

    problem!!!!

    want to search for folders with a specified name and delete them with contents.
    “del private_pictures /s” command fails
    “rd private_pictures /s” command fails

    does anybody have any ideas (win7/vista)

  • kapil

    i want to delete a folder and sub folders and file coming 20 day

  • stubow75

    Can you use the command del /S /P *.jpg but for different file types, and how would you delimite the file types e.g. del /S /P 8.jpg, *.avi, *.txt

  • http://pinoybizness.com junnydc

    this is really cool. But how to delete read only files. its says access denied for those files.

  • Dinesh

    Awesome! I have tried this now and it really works. Thanks.

  • Praveen

    can anybody help me to all delete sub folder with files….
    folder name like inputfile* that contains some csv,txt files….
    folder structrure
    ./main/batchfile
    ./main/output/inputfile1*
    ./main/output/inputfile123*
    ./main/output/inputfile34*
    ./main/logs/*.csv

  • http://www.techntoday.com/ sahil

    thanks for sharing this
    it really a lot to me

 
Copyright 2006-2012 Techie Buzz. All Rights Reserved. Our content may not be reproduced on other websites. Content Delivery by MaxCDN