On Windows computers, we can delete files from command prompt using the command Del. Below you can find syntax of this command. This command can be used with the same syntax in batch scripts.
Delete a single file from command line:
The basic syntax of this command is as follows.
Del command does not print anything upon the successful deletion of files. See the example below.
D:\>dir /s /b 1.pdf D:\>del 1.pdf D:\> D:\>dir /s /b D:\>
Delete multiple files from command line
We can specify multiple file names with the del command.
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 all files in a directory
You can do this by specifying relative/absolute path of the directory with the del command.
c:\>dir /s /b data c:\data\1.pdf c:\data\2.pdf c:\data\3.pdf c:\>del data c:\data\*, Are you sure (Y/N)? y c:\>dir /s /b data c:\>
Delete Read only files
If you run del command on a read-only file, you would get access denied error.
E:\>attrib 1.pdf A R E:\1.pdf E:\>del 1.pdf E:\1.pdf Access is denied. E:\>
We can delete a read-only file by adding /F flag.
E:\>attrib 1.pdf A R E:\1.pdf E:\>del /F 1.pdf E:\>
Using wild cards with del command
We can use ‘*’ with del command. For example to delete all jpg files in the current directory, we can use the below command.
Delete in quiet mode, skip confirmation
You can add /Q flag when you are deleting files with global wild card ‘*’ or deleting all the files in a directory.
E:\>del * E:\*, Are you sure (Y/N)? n E:\>del /q * E:\>
Confirm deletion for each file
When you are deleting all the files in a directory, del command would ask for confirmation only once. This way, you can say ‘yes’ either for all the files or for none of them. If you want to provide that input for each file differently, then you can add /P flag.
E:\>del /p *.pdf E:\1.pdf, Delete (Y/N)? y E:\2.pdf, Delete (Y/N)? n E:\3.pdf, Delete (Y/N)? y
This command works on all Windows editions. If you have any questions feel free to write in the comments.