This post explains how to delete files from command prompt on Windows computers. Before we start to look at the syntax of ‘del’, note that this command can delete only files, it can’t delete folders.
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 if something goes wrong.
Delete multiple files
We can delete any number of files with
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 files in bulk using Wildcards
Del command recognizes wildcard(*) and so can be used to operate on files in bulk. 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 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
It should be noted that del command can’t handle directories. When you run this command on a folder, what it would actually do is to delete all the files in the folder.
C:\>del Documents C:\Documents\*, Are you sure (Y/N)? n