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 on Windows 11 & 10 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

Advertisement
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

Delete files with the system attribute

We can delete files having the system attribute set by using /A:S with the del command. This option works on Windows 11 & 10 too. It refers to the file attribute, not a way to bypass protection on Windows operating system files.

For example, if C:\dir\convert.exe has the system attribute set, a normal dir command may not list it. We can check the file and its attributes using the below commands.

dir /A "C:\dir\convert.exe"
attrib "C:\dir\convert.exe"

To delete this file, run the below command. The /P option asks for confirmation first.

del /A:S /P "C:\dir\convert.exe"

The del command does not move files to the Recycle Bin, so back up any file you may need before deleting it. Do not use this example to remove files from the Windows folder. The /A:S option does not override file permissions or remove locks held by programs using the file.

Advertisement

15 thoughts on “Delete files from command line”

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

    Reply
  2. 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.

    Reply
  3. 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

    Reply
  4. 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

    Reply
  5. 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*

    Reply
  6. 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.

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

    Reply
  8. 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.

    Reply
    • 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”

  9. You can install unlocker on your system. You will get its option in context menu. All the files you elect to delete should be deleted at next boot time.

    Reply
  10. I know this file that I’m trying to delete from different drives in my computer is some sort of maleware or other virus. I tried your method but I get this command line back: “Access Denied” (without the quotes). Can you tell me where do I go from here? Appreciate your help.

    Reply

Leave a Comment