≡ Menu

Delete files from command line

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!

del filename

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'

del filename1 filename2 filename3 filename4....

Example:

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

del *

To delete all the files with ‘log’ extension

del *.log

Delete all files having the prefix ‘abc’

del abc*

Delete all files having ‘PIC’ somewhere in the file name.

del *PIC*

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

Deleting folders

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
4 comments… add one
  • Garg

    Is there any batch command to delete all zero byte sized files from cmd.

  • ClayStation

    There is a way to remove folders from the commandline, use “RD” (remove directory), and it has a couple of silent switches, “RD /S /Q C:/Directory” will remove C:/Directory silently, I use this all the time. I think that the directory path has to be in quotations if it contains any spaces.

  • ClayStation

    UAC might want to fight over deleting files, in that case, the command “takeown” to give you ownership of the file first, should get around that.

  • Abigail Ava

    Sometimes we get an error when we try to delete a File or a folder for no reason , but of course there is a reason.We have many damage file or blocked files.Do not worry if we want to remove the error files or too long path files from our system,here I suggest a smooth way.So use “Long path tool” software and keep yourself.

Leave a Comment