DEL and DELTREE

By | 2005-11-01

In MS-DOS, DEL and DELTREE are commands used for deleting files and directories, but they have some key differences in how they operate.

DEL (Delete)

  • Purpose: Deletes individual files or a group of files in a directory.
  • Usage: You can delete one or more files by specifying their names or use wildcards like * to delete multiple files at once.
  • Example:
    • DEL file.txt deletes file.txt.
    • DEL *.txt deletes all files with the .txt extension in the current directory.

The DEL command does not delete directories; it only works on files. If you try to delete a directory using DEL, it will return an error.

DELTREE (Delete Tree)

  • Purpose: Deletes a directory and all its contents, including subdirectories and files.
  • Usage: This command is more powerful since it allows you to delete an entire directory structure in one go.
  • Example:
    • DELTREE foldername deletes the foldername directory and everything inside it.

Unlike DEL, DELTREE will remove both directories and files, making it more destructive if used without caution. It’s especially useful for removing directories that are deeply nested.

In modern systems, these commands have been mostly replaced by safer and more user-friendly options, but they were essential in managing files and directories in MS-DOS.

DEL

Delete one or more files.

Syntax

DEL [options] [/A:file_attributes] files_to_delete

Key

Switch/dataWhat it does
files_to_deleteThis may be a filename, a list of files or a Wildcard options
/PGive a Yes/No Prompt before deleting.
/FIgnore read-only setting and delete anyway (FORCE)
/SDelete from all Subfolders (DELTREE)
/QQuiet mode, do not give a Yes/No Prompt before deleting.
/ASelect files to delete based on file_attributes

file_attributes

SwitchWhat it does
RRead-only
-RNOT Read-only
SSystem
-SNOT System
HHidden
-HNOT Hidden
AArchive
-ANOT Archive

Wildcards

These can be combined with part of a filename

  • * Match any characters
  • ? Match any ONE character

Examples

To delete HelloWorld.TXT

DEL HelloWorld.TXT

To delete Hello Big World.TXT

DEL "Hello Big World.TXT"

To delete all files that start with the letter A

DEL A*

To delete all files that end with the letter A

DEL *A.*

To delete all files with a .DOC extension

DEL *.DOC

To delete all read only files

DEL /a:R *

To delete all files including any that are read only

DEL /F *

Files are sometimes created with the reserved names: CON, AUX, COM1, COM2, COM3, COM4, LPT1, LPT2, LPT3, PRN, NUL. To delete these use the syntax:

DEL \\.\C:\somedir\LPT1

Folders

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.

Temporary Files

You should clear out TEMP files on a regular basis – this is best done at startup when no applications are running. To delete all files in all subfolders of C:\temp\ but leave the folder structure intact:

DEL /F /S /Q \%TEMP\%

When clearing out the TEMP directory it is not generally worthwhile removing the subfolders too – they don’t use much space and constantly deleting and recreating them can potentially increase fragmentation within the Master File Table. Deleting a file will not prevent third party utilities from un-deleting it again, however 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

Notes

ERASE is a synonym for DEL.

If Command Extensions are enabled (default) DEL /S [path]filename(s) will display a list of the files deleted. If Command Extensions are disabled: DEL /S [path]filename(s) will display a list of any files it cannot find.

DELTREE

Previous versions of Windows had the DELTREE command that deletes all files and sub folders, DEL /s will delete all files, RD /s will remove all files and folders including the root folder.

The following command script will also provide DELTREE functionality:

@echo off
pushd \%1
del /q *.*
for /f "Tokens=*" \%\%G in ('dir /B') do rd /s /q "\%\%G"
popd

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.