Deleting files is one of the frequently done operation from Windows command prompt. This post explains how to use ‘del’ command from CMD for different use cases like deleting a single file, deleting files in bulk using wild cards etc. Before we start to look at the syntax, note that the command works only for files and can’t handle folders.
How to delete a file
Run del command with the name of the file to be deleted, you are done!
You do not see message after running the command if the file is deleted successfully. Error message is shown only when something goes wrong.
Delete files in bulk
Del command recognizes wildcard(*) and so can be used to delete files in bulk from CMD. Some examples below.
To delete all the files in current folder
To delete all the files with ‘log’ extension
Delete all files having the prefix ‘abc’
Delete all files having ‘PIC’ somewhere in the file name.
The above are the basic use cases of del command. Continue to read below for non trivial use cases.
Delete multiple files
‘Del’ command can accept multiple files as argument
del filename1 filename2 filename3 filename4....
D:\>dir /s /b 1.pdf 2.pdf 3.pdf D:\>del 1.pdf 2.pdf 3.pdf D:\> D:\>dir /s /b D:\>
Delete Read only files
We can’t delete a read-only file using simple‘del’ command. We get access denied error in this scenario.
c:\>attrib readonlyfile.txt A R C:\readonlyfile.txt c:\>del readonlyfile.txt c:\readonlyfile.txt Access is denied. c:\>
A read-only file can be deleted by adding /F flag.
del /F readonlyfile.txt
Alternatively, we can use the below command too
del /A:R readonlyfile.txt