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 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 /Sdirective 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.