del/erase - Delete files
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.
By executing this command, the sample.txt
file will be deleted.
Options for the del
command
The del
command has the following options.
Option | Description |
---|---|
/p | Displays a confirmation message before deleting |
/f | Forces deletion of read-only files |
/s | Displays the names of the deleted files on the screen after deletion |
/q | Does 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.
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.
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.
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.