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:ABC” and 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 “cmd” without 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 “/S” directive tells the del command to delete all the files recursively from within the folder and sub-folders. See screenshot below.

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.

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.





Sandip
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.
Reply to this comment
Nathan
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
Reply to this comment
Gert-Jan
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.
Reply to this comment
Pigeon
February 19th, 2009 at 12:12 pm #
Awesome, was looking for something to do this for a while.
Thanks a lot
Reply to this comment
Jaymie
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).
Reply to this comment