≡ Menu

Delete files from command line

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!

del filename

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

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 multiple files

‘Del’ command can accept multiple files as argument

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

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

    • Aman Soni

      Iterate recursively over the files:

      for /r %F in (*)
      Find out zero-length files:

      if %~zF==0
      Delete them:

      del “%F”
      Putting it all together:

      for /r %F in (*) do if %~zF==0 del “%F”
      If you need this in a batch file, then you need to double the %:

      for /r %%F in (*) do if %%~zF==0 del “%%F”

  • 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.

  • Austen

    Well done, I used this when del *1*.*

    Will keep this in my reference book. Thanks a lot!

  • Gigi

    Is there a way to exclude filenames matching a pattern from a delete? For example, I want to delete all files except those whose names are like *xyz*

  • Bethany Ben

    I am trying to delete csv files in DOS that have a date suffix of earlier
    than 2 months ago. I need to do this dynamically, from a batch file.
    Is this possible ? Many thanks.
    Bethany

  • Floyd

    I want to delete a file from all the user’s profiles using cmd.
    The file is on the desktop.

    Name of the file-Hello My Name

    I’m writing del %PUBLIC%\Desktop\Hello*My*Name.pdf,but that doesnt seem to work.
    I also tried del %PUBLIC%\Desktop\*Hello* but that didnt work too.

    Any help is appreciated

  • Roshan Kandel

    c. Using the for loop create 5 text files which start from letter a (ex: a1, a2, a3, a4, a5).

    can you help me
    create 5 files like this command.
    for /l %a in (1 1 5) do mkdir File%a.

  • damien brock

    Ever have a problem not being able to delete a file or folder because the filename is invalid. I used the Long Path Tool to fix it

  • cONSiceBotles

    How do you delete a folder with command line?

Cancel reply

Leave a Comment