To delete a file with a Windows script, it is recommended to use the cmd del command. The del or delete command provides many very useful options to be in control of the deletion process. The Command Prompt is a text-based user interface that enables users to execute commands and perform scripted tasks with their computer.
It’s a powerful tool that can be used in a variety of ways. Users can use the Command Prompt to copy files, delete files, manage the registry and much more.
Table of Contents
Delete files with Windows command prompt
It’s also an easy way to execute commands without using the graphical user interface (GUI). In this article, we will show you how to delete files with the Windows command prompt and why you should start using it. It is a part of the administration commands available in MS systems.
To delete a file with cmd, you need to provide the exact path of the file. If you don’t know where the file is located, you can use the dir command to locate it.
The main option that i suggest using is the deletion confirmation. This way you are able to control one last time which file top delete. Please note that the files deleted with the del command line are not stored in the Recycle Bin. So you cannot restore them.
Make sure you delete the proper file. The delete operation is irreversible.
Remove files the simple way
To delete a file in cmd and use the explicit path, without any confirmation prompt, use this command:
del "C:\Folder\file (1).txt"
Delete files with a confirmation prompt
The MS-DOS tool can help you delete multiple files with a confirmation for example. To delete a file and make sure to double check the filename.
del /p "C:\Folder\file (2).txt"
Then a prompt is displayed:
C:\Folder\file (2).txt, Delete (Y/N)?
Use the delete command without confirmation
The other way is to disable the confirmation for a large number of files for example. To force the deletion with no prompt at all use the delete in quiet mode:
del /q "C:\Folder\file (3).txt"
Delete a read only file with cmd force option
When deleting a read only file, the command prompt displays this error message, because the file is read only:
Access is denied.
To force deletion of a read only file, and bypass the read-only attribute, use the del /f option:
del /f "C:\Folder\file (3).txt"
The deletion command offers multiple options
To conclude, the “del /p” option is definitely the best cmd option to make sure to delete the proper files from the disk. Use it when only when deleting specific and targeted files. To delete a large number of files, use the quiet and the force options.
Check out how to copy a file to another folder in command line.