How To Delete Files & Folders Using The Windows Command Line (CMD)

Cmd.exe is the command-line interpreter for Windows operating system. By using the command prompt, one can navigate to any file located on the computer.

As per the default Windows 7/8/10 installation, the command line will open the user’s home directory. This directory is usually “C:\Users\<username>” where “<username>” is the username selected during Windows setup.

Windows Command Line Delete Folder Prompt

Showing the contents of a directory

This folder holds the documents, pictures, desktop and downloads directories associated with the user. To list and display all files and folders in the command line, type “DIR” (short for directory). This will list all files and folders and associated attributes of those files like size, file attributes, created and modified date, etc.

How can you navigate to another folder through CMD

The first command in this aspect is the “cd” (Change Directory). The command helps the user in changing the current directory or folder from the current directory.

Note: Windows File System and Commands are not case sensitive. Hence, “cd” is the same as “CD”.

Windows Command Line Delete Folder CD Change Directory

Type “cd Desktop” to navigate the “Desktop” folder. This will show all the items on your Windows Desktop. Since only the folder name “Desktop” was specified, it is considered a relative folder name. Meaning Windows will look for a “Desktop” folder in the current folder only. Sub-folders or “Desktop” folders outside of the current folder are ignored.

To specific absolute folders, specify the full folder path. For example, “C:\Users\<username>\Desktop”.

It is possible to specify absolute folders starting from the correct location. Use the specific “.” operator to start from the correct folder and enter the remaining folder path after the dot. For example, “cd ./Documents”

The command “cd \” changes the directory to the root folder of the drive. If you are on the “C” drive, then “cd \” will take you to “C:\>” irrespective of the current folder.

If you want to navigate up to a folder, you will have to use the cd.. command.

Say you want to go to the “Users” folder. Assuming you are in the default folder “C:\Users\<username>”, type “cd..” and press Enter. This will change the current folder to “C:\Users”.

How to Delete Directories or Folders with CMD?

In the command line (cmd.exe), we use the “rmdir” command for deleting a folder. “rmdir” is short for “Remove Directory”. It is worth noting that “rmdir” deletes only directories. It will show an error if you try to erase a file with the command “rmdir”.

Type “rmdir <foldername>” to delete the folder. Take extra care behavior executing this command since you may lose important data.

If “rmdir” is used sans parameters, it will only remove empty folders.

Windows Command Line Delete Folder RMDIR remove directory

Using the “/s” parameter, we can erase a folder, sub-folders, and files inside the specified folder. For example, “rmdir /s c:\temp” will remove all files and folders inside the folder “temp” residing in the root directory “C:\”.

As it is a matter of deletion, the command line reconfirms if you want to really delete the folder/files. If you type “y”, the computer will take that to be the confirmation and continue to delete the specified folder.

If the folder to be deleted has a space in its name, enclose the name in double quotes like this: “rmdir “My Folder With Space In Name””

To delete a folder in a directory that is not owned by you, you should opt to start the command line tool as an administrator using the “Run as administrator” option. 

To “Run as administrator”, follow these steps:

  1. Press the “Windows” key to open the Windows “Start” menu
  2. Type “cmd” to search for the command-line tool
  3. “Command Prompt” should show up as the first search result
  4. Right-click on it and select “Run as Administrator”

While working with administration privileges and deleting files or folders, an extra level of caution is required as any wrong command can permanently crash your computer.

Deletion of Files with CMD

“del” command is used for deleting files in the command line tool (cmd.exe).

If you try to delete a file using “rmdir”, it will show an error. Similarly, if you try to delete a folder using the “del” command, it will also result in an error.

You need to just type “del” followed by filename and extension, to delete the file. For example, type “del temp.txt” to delete a file named “temp.txt” in the current folder.

Windows Command Line Delete Folder DEL delete file

You can also specify the file’s full path if the file to be deleted is not in the current folder.

Like “rmdir”, the “del” command will require administrator privileges if the file to be deleted and not owned by the current user.

If you want to delete multiple files at one go, you will have to enter the filenames consecutively. For example, “del temp.txt temp1.txt”.

Delete multiple files and folders at one go.

It is not always possible to enter all the file names or folder names while deleting them. It can be impractical due to multiple causes, and it doesn’t make any sense if there is a better way to do that.

The command-line tool supports wildcards that can act as placeholders and match multiple files or folders.

Asterix (*) is the most commonly used wildcard in the command-line tool. Asterix matches one or more characters. For example, “pop*” matches all names that start with “pop”. It will match “popcorn”, “popup”, and “pop corn”.

Similarly, “*pop*” matches “butter popcorn”, “repop” and “popcorn”. Essentially, matching anything that contains the word “pop” in it.

Type command “del pop*” will delete all matching files starting with the word “pop”.

To delete all text files, type “del *.txt”. This will match and delete all files that have the extension “.txt”.

Similarly, “rmdir /s *.*” will delete all folders, sub-folders, and files in the current folder. Be extremely careful while executing this command.

Summary

  • For navigating straight to the directory where the file is residing, you need to use the “cd” and “dir” commands.
  • To perform deletion of folders, use “rmdir” and delete files, use the “del” command. The folder or file name should be in quotes if it contains space in its name.
  • Wildcards can be used to delete multiple files and folders that match a specific pattern.

Leave a Reply

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