Recursively Delete Files in Folders and Sub Folders Using Command Prompt

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.

  • Share

Related Topics Similar to This Article: »

Recent Activity: »

Comment Using Facebook

25 Responses to this Article | Share your Opinions/Comments

We moderate comments to prevent spam. Moderation is done within few hours. Please try and stay on topic and refrain from using abusive language. If you think there is a problem with this post, please email the post author or send us an email at tips@techie-buzz.com with the URL and the problem you see and we will rectify it as soon as we can.

  1. Sandip on July 29th, 2008 at 11:56 pm #

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

  2. Nathan on September 30th, 2008 at 4:04 pm #

    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

  3. Gert-Jan on February 9th, 2009 at 6:17 am #

    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.

  4. Pigeon on February 19th, 2009 at 12:12 pm #

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

    Thanks a lot

  5. Jaymie on August 11th, 2009 at 10:38 am #

    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 on February 3rd, 2010 at 5:25 am #

      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 on July 1st, 2010 at 11:34 am #

      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>

  6. antmaster on February 22nd, 2010 at 10:02 pm #

    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.

    • Keith Dsouza on February 22nd, 2010 at 10:14 pm #

      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.

  7. Bob on May 15th, 2010 at 7:34 am #

    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. :-(

    • Keith Dsouza on May 16th, 2010 at 9:14 am #

      @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

  8. mareike on August 27th, 2010 at 1:54 pm #

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

  9. Precious on September 13th, 2010 at 6:29 am #

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

  10. Hond on October 1st, 2010 at 1:59 am #

    very good instruction and thanks.

  11. Sandeep Singh on November 11th, 2010 at 11:12 am #

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

  12. obeida ghazal on November 17th, 2010 at 5:06 pm #

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

  13. silver on March 22nd, 2011 at 5:02 am #

    how find the deleted files or folder using cmd

  14. Gilbert Lee on August 28th, 2011 at 4:38 pm #

    Try rm_folder tool.

    http://www.gilgil.net/9075

    example : rm_folder .svn

    Thank you.

  15. bob on October 3rd, 2011 at 3:22 pm #

    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!?

  16. g-man on October 6th, 2011 at 10:37 am #

    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)

  17. kapil on October 8th, 2011 at 1:07 pm #

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

  18. stubow75 on November 29th, 2011 at 7:19 pm #

    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

  19. junnydc on January 31st, 2012 at 9:34 pm #

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

  20. Dinesh on April 10th, 2012 at 3:13 pm #

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

  21. Praveen on May 20th, 2012 at 10:47 am #

    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