DEL

Delete one or more files.

Syntax
      DEL [options] [/A:file_attributes] files_to_delete

Key
   files_to_delete : A filename or a list of files, may include wildcards.

   options:
      /P  Give a Yes/No Prompt before deleting. 
      /F  Ignore read-only setting and delete anyway (FORCE).
      /S  Delete from all Subfolders (DELTREE).
      /Q  Quiet mode, do not give a Yes/No Prompt before deleting.

      /A  Select files to delete based on file_attributes
            file_attributes:
              R  Read-only    -R  NOT Read-only
              A  Archive      -A  NOT Archive
              S  System       -S  NOT System
              H  Hidden       -H  NOT Hidden
              I  Not content indexed.  -I  Content indexed files.
              L  Reparse points.       -L  NOT Reparse points.

              X  No scrub file attribute.  -X  Scrub file attribute.
              V  Integrity attribute.      -V  NO Integrity attribute.

   Wildcards: These can be combined with part of a filename

      *  Match any characters
      ?  Match any ONE character

If a folder name is given instead of a file, all files in the folder will be deleted, but the folder itself will not be removed.

Errorlevels: DEL will return an Errorlevel of 0, irrespective if the delete succeeds or fails for any reason.
Also a deletion failure will not fire for || unless the DEL arguments are invalid.
If you delete files using PowerShell then a True/False return code ($?) will be set correctly.

Errorlevels

If the files were successfully deleted %ERRORLEVEL% = 0
If the files failed to delete, or don’t exist to be deleted %ERRORLEVEL% = 0
Bad or no parameters given %ERRORLEVEL% = 1

Undeletable Files

Files are sometimes created with a very long filename or a trailing period or with reserved names (CON, AUX, COM1, COM2, COM3, COM4, LPT1, LPT2, LPT3, PRN, NUL) and as a result they become impossible to delete with Windows Explorer.

To delete such files use the Device Path: DEL "\\?\path to file"

e,g,
DEL "\\?\C:\some folder\AZH64GT."

Alternatively for long filenames, you can reduce the total path length by using SUBST to map a drive letter to the folder containing the file.

It is also possible to delete long paths using RoboCopy - Copy/Move the required files to a temporary folder and then delete the folder, one gotcha with that technique is RoboCopy's tendency to follow symbolic links which can cause files outside the source folder to be moved/ deleted.

If a file is still 'undeletable' this may be caused by the indexing service, temporarily stop the service and then delete the file.

Thumbs.db

If you are unable to delete thumbs.db using Windows Explorer or while Explorer is open, this is a bug in the thumbnail view for images. This issue can occur for folders which don't even contain any images.
To resolve, open Explorer, select View ➞ Options, open the "View" tab and uncheck "Display file icon on thumbnails".
For a more permanent registry fix, you can disable thumbnail caching completely.

Permanent deletion

Deleting a file will not prevent third party utilities from un-deleting it again. Secure file deletion utilities are available, however for casual use, you can turn any file into a zero-byte file to destroy the file allocation chain like this:

TYPE nul > C:\examples\MyFile.txt
DEL C:\examples\MyFile.txt

Delete Locked files

Typically this is caused by the Offline Cache or Internet Explorer temp files.

Close all applications
Open a command prompt
Click Start, and then Shut Down
Simultaneously press CTRL+SHIFT+ALT.
While you keep these keys pressed, click Cancel in the Shut Down Windows dialog box.
In the command prompt window, navigate to the cache location, and delete all files from the folder (DEL /s)
At the command prompt, type explorer, and then press ENTER.

DELTREE - Older versions of Windows had the DELTREE command to delete all files and sub folders. This can be replicated with a script as shown on the DELTREE page.

Examples

Delete the "Hello World.txt" file:

DEL "Hello World.txt"

Delete 3 named files:

DEL file1.txt file2.txt "C:\demo\file3.txt"

Delete all files that start with the letter A

DEL A*

Delete all files that end with the letter A

DEL *A.*

Delete all files with a .doc extension:

DEL *.doc

Delete all read only files:

DEL /a:R *

Delete all files including any that are read only:

DEL /F *

Normally DEL will display a list of the files deleted, if Command Extensions are disabled; it will instead display a list of any files it cannot find.

DEL is an internal command. ERASE is a synonym for DEL

“But over all things brooding slept, The quiet sense of something lost” ~ Alfred Tennyson

Related commands

CIPHER /w - Overwrite data in a directory (secure delete).
DELPROF - Delete user profiles.
DELTREE - Script to Delete a folder and all subfolders/files.
RD - Delete folders or entire folder trees.
CleanMgr - Automated cleanup of Temp files, Internet files, downloaded files, recycle bin.
FORFILES - Delete files older than X days.
INUSE - updated file replacement utility (may not preserve file permissions).
Q120716 - Remove Files with Reserved Names.
Q320081 - You cannot delete a file or folder.
Q159199 - A file cannot be deleted (NTFS).
PowerShell: Remove-Item - Delete the specified items.
Equivalent bash command (Linux): rmdir / rm - Remove folders/ files.


 
Copyright © 1999-2024 SS64.com
Some rights reserved