How To Delete A File Using CMD (With Advantages And Tips)

By Indeed Editorial Team

Published 14 May 2022

The Indeed Editorial Team comprises a diverse and talented team of writers, researchers and subject matter experts equipped with Indeed's data and insights to deliver useful tips to help guide your career journey.

Command prompt or cmd is an application that works as a command-line interpreter, allowing users to perform many actions from a central location. If you have trouble deleting a file or folder directly by right-clicking, you can use the command prompt to delete it. Knowing how to delete a file using cmd can be helpful for IT professionals. In this article, we discuss the advantages of deleting a file using cmd, explore the steps of deleting a file and folders on Windows and understand some tips for erasing a file.

What is a Windows command prompt?

A command prompt is a Windows application that communicates with the operating system. It is a text-based user interface screen that performs every function of a navigation tool. IT professionals use a command prompt for conducting advanced administrative activities, executing batch files, troubleshooting issues in Windows, solving computer issues and running diagnostics. Cmd gives you a way of interacting with a computer directly using text commands.

Interestingly, the commands you use in the command prompt are not case-sensitive. So, focus on entering the commands accurately. You can use the command prompt to trace a hacker, delete a file, create a folder, erase a folder and possibly fix a file that is not opening. Also, you can use only the arrow keys to scroll through the commands in the cmd.

Related: 7 Common Desktop Support Interview Questions (With Answers)

How to delete a file using cmd

To understand how to delete a file using cmd, follow these steps:

1. Open the command prompt

Press the Windows key and type ‘cmd' to open the command prompt. Next, click on ‘Run as administrator'. If you cannot open the cmd prompt as an administrator, you can open a cmd window by clicking ‘Open' instead of ‘Run as administrator'. The only difference would be that you cannot delete some protected files. Another way to start the command prompt is to press the Windows key and the' R' button, type 'cmd' and click on 'OK'. Using Windows key + R, you can quickly launch any app as an administrator.

The cmd screen can look like this:

C:\Users\Reyansh

2. Change the path of the directory in cmd

In the next step, change the path of the directory in the cmd and set it to the path of the file you want to delete. For instance, if you want to delete a file in the Documents folder, type the following command in the cmd.

cd documents

The cmd screen looks like this:

C:\Users\Reyansh\Documents

Repeat this process until you reach the folder where you saved your file.

3. Use the delete command

After reaching the desired folder, use the del command, followed by the file name.

The syntax for deleting a file is: del “filename"

For instance, if you want to delete Math_file.txt, run the following command:

C:\Users\Reyansh\Documents> del “Math_file.txt”

If you receive a prompt to delete the file, type ‘Y' and press ‘Enter'.

Remember that you cannot recover any files you delete using the del command. So, be careful of using the delete command.

4. Erase read-only files

When users mark files as read-only, it becomes difficult to delete the file. When you try to delete such a file using a cmd, it gives the error 'Access is denied'. For such files, you can use the force delete command.

The syntax for force deleting a file is: del /f “filename"

For instance, if you want to delete the read-only file Math_file.txt, run the following command:

C:\Users\Reyansh\Documents> del /f “Math_file.txt”

5. Erase files in bulk

Using the command prompt, you can use the delete command to erase files in bulk. The delete command recognises the wildcard (*). For instance, if you want to delete all files with the .log extension, run the following command:

C:\Users\Reyansh\Documents> del *.log

You can even remove files with the same prefix. For instance, if you want to delete all files with the prefix Home, run the following command:

*C:\Users\Reyansh\Documents> del home**

The cmd allows you to delete files with particular letters in the filename. For instance, if you want to delete all files having the word end somewhere in the filename, run the following command:

*C:\Users\Reyansh\Documents> del *end**

6. Erase multiple files

You can erase multiple files using a single command using the delete command. The syntax for deleting multiple files is:

del file1 file2 file3 file4

For instance, if you want to delete multiple files, run the following command:

C:\Users\Reyansh\Documents> del home.txt house.pdf hotel.pdf

What are the advantages of deleting a file using a command prompt?

The command prompt lets a professional delete file using simple instructions. It lets you delete files from a system without a trace. With a cmd, you can erase a single file or multiple files from a local disk on the hard drive without spending too much time. Using a cmd, you are not deleting a file but removing the indexing capability, ensuring that the file is invisible to the operating system.

This means that the deleted file is not present in the recycle bin. Another advantage of deleting a file using cmd is that it allows you to delete files with a particular extension or remove read-only files.

Related: What Does An IT Professional Do? Duties And Requirements

How to delete folders using the cmd

To delete a folder and all the files in it, use these steps:

1. Open the command prompt

Press the Windows key and type ‘cmd' to open the command prompt. Next, click on ‘Run as administrator'. If you cannot open the cmd prompt as an administrator, you can open a normal cmd window by clicking ‘Open' instead of ‘Run as administrator'. The only difference would be that you might not delete some protected folders. Another way to start the command prompt is to press the Windows key with the 'R' button, type 'cmd' and click on 'OK'. Using Windows key + R, you can quickly launch any app as an administrator.

The cmd screen can look like this:

C:\Users\Reyansh

2. Change the path of the directory in cmd

In the next step, change the path of the directory in the cmd and set it to the path of the file you want to delete. For instance, if you want to delete the Documents folder, type the following command in the cmd.

cd Documents

The cmd screen looks like this:

C:\Users\Reyansh\Documents

Repeat this process until you reach the folder you want to erase.

Related: 17 Active Directory Interview Questions And Example Answers

3. Use the rmdir command

Using the rmdir command, you can erase an entire folder or directory. For instance, if you want to erase the Documents folder, type the following command in the cmd:

rmdir “Documents”

The above command would remove the documents folder only if it is empty. If any folder has sub-directories, you can get the prompt, 'The directory is not empty.'

To remove such folders, use the option of recursive deletion of items. Use the following command in the cmd:

rmdir /s “Documents”

If you receive a prompt to delete the file, type ‘Y' and press ‘Enter'.

/s removes all directories and files in a particular directory in addition to the directory itself. Professionals use it for removing a directory tree. If you do not want to see the prompt, use the command /q. The /q is a quiet mode that does not ask if you can remove a directory tree. Use the following command in the cmd:

rmdir /s /q “Documents”

Related: 20 Best Computer Science Jobs In India (With Salaries)

Tips for deleting command using command prompt

Here are a few tips you can follow for deleting a file using cmd:

  • Run the cmd as an administrator. This is a desirable step because it allows you to delete any file or folder on the computer.

  • Use the change directory command to switch directories. To reach the destination of the file or folder you want to delete, use the change directory or cd command in the cmd.

  • Use the cd.. command to move back one directory. If you enter a wrong directory name, focus on using the cd.. command because it helps you move back one directory. You can use cd.. twice to move back two directories.

  • Get more information about a command. When deleting a file, if you have any doubt about the del or rmdir command, you can use ‘/?' at the end of the command. This gives you information about how that command works.

Please note that none of the companies, institutions or organisations mentioned in this article are associated with Indeed.

Explore more articles