In this post, I will show how you can easily delete unwanted files within a folder or subfolders based on its extension and attribute. You can even automate this process with Task Scheduler for regular cleaning up.
Before starting, do note that files deleted from command line are permanently deleted, which means deleted files are not stored in Recycle Bin.
Delete Specific File Type from a Folder and its Subfolders
DEL "C:\Folder\*.exe" /S /Q
This command will delete all EXE (executable) files within the specified directory and its sub directories. /S
parameters checks inside subdirectories, and /Q
deletes files silently without prompting.
Delete All Files Inside a Folder and Subfolders
DEL "C:\Folder\*.*" /S /Q
This command is useful if you want to delete all files within a directory and its subdirectories. File extension is not checked here.
Delete Files Based on Attribute
Suppose you want to delete files that are having a specific attribute, you can do that with this command:
DEL "C:\Folder\*.*" /S /Q /A:R
This delete all files having Read-Only attribute.
Windows attributes list:
R
: Read-only filesS
: System filesH
: Hidden filesA
: Files ready for archivingI
: Not content indexed filesL
: Reparse Points
Delete Files that does not have a Specific Attribute
DEL "c:\MyFolder\*.*" /S /Q /A:-S
With this command you can exclude files having a specific attribute. This can be handy, for example, if you don’t want to delete files with System attribute.
Delete All Sub Folders and its content
for /D %%X in ("C:\Folder\*") do RD /S /Q "%%X"
This is a quick command that will recursively delete all subdirectories.
Automate Deletion
Using Windows task scheduler, you can schedule any of the above deletion command to run as frequently as you like.
CMD.EXE /C "DEL C:\Folder\*.exe"
That’s all! No need to use third-party tools for simple tasks as this.