del/erase - Delete files

Maintained on

When working with command prompt or batch files, it is common to manipulate files and folders on your PC.

However, there are different commands to use depending on whether you want to delete a file or a folder, and whether the folder is empty or not.

Here, we explain how to delete files, from basic usage to setting options, in an easy-to-understand way.

To delete a folder, you will need to use a different command. For more information on deleting folders, please refer to the following article:

How to delete files

To delete a file, use the del command. You can also use the erase command, which will produce the same result.

The del command is used as follows:

del [/p] [/f] [/s] [/q] [/a[:]<file_attributes>] <file_name>

The most basic usage is to specify the path of the folder you want to delete after del.

The following command is an example of deleting the sample.txt file created directly under the user directory.

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>del sample.txt

By executing this command, the sample.txt file will be deleted.

Options for the del command

The del command has the following options.

OptionDescription
/pDisplays a confirmation message before deleting
/fForces deletion of read-only files
/sDisplays the names of the deleted files on the screen after deletion
/qDoes not display a confirmation message
a:Deletes only files with the specified attributes.

/p option

When you specify the /p option, a confirmation message is displayed before deletion.

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>del /p sample.txt
Delete C:\Users\user\sample.txt (Y/N)?

If a confirmation message is displayed, enter Y to execute the deletion.

How to delete files with examples

Deleting files with specified attributes

To delete files with specific attributes, such as system files or hidden files, use the /a option.

The following example deletes the sample.txt file that is both hidden and read-only.

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>del /a:HR sample.txt

Deleting files using wildcards

With the del command, you can delete multiple files at once by using wildcards.

The following example deletes files that start with sample created directly under the user directory.

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>del sample*

Deleting files in subfolders

With the del command, you can also delete files in subfolders.

The following example deletes files in the sample folder created directly under the user directory.

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>del sample\\*

Quiz

Quiz

Which command displays a confirmation message before deleting?

#Command Prompt #Batch Files #Command Line #Commands