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

Navigating To The File Using CMD

To delete a file or folder using the command prompt we must first navigate to the location of our file.
By default the command line is located at a folder named after your computers username (C:\Users\MyPC). This folder contains your documents, downloads and desktop directories, among others. To view all the files in this directory use the “Dir” command, which stands for directory.

Dir

Every file in this directory will be displayed at once. If the file or directory you want to delete is located at your desktop for example use the “Cd” command to change the command prompts directory. Simply type “Cd” followed by the folder you want to navigate into within quotes.

Cd "Desktop"

You will at once navigate into the desktop directory and from here you can use the “Dir” command once again to locate the file or folder you want to delete.
If you navigated to the wrong directory by mistake, type:

CD ..

You will immediately be navigated into the previous directory.In a similar way you can use a combination of the “Dir” and “Cd” commands to navigate to any directory and locate any file or folder.

Deleting Folders / Directories Using CMD

To delete a folder use the “Rmdir” command, which stands for “Remove Directory”.
Keep in mind that as the name suggests, “Rmdir” only deletes directories. Trying to delete a file using “Rmdir” will constitute in an error.

Rmdir "My Folder"

Rmdir” without any parameters only removes folders that are empty, to delete a folder along with the files or folders within it you need to use the /S parameter.
The command would look like this:

Rmdir /S "My Folder"

After entering the command the command line will ask if you are sure you want to delete this file. Simply press “y” to confirm.
If you want to delete a folder in a directory other than your users folder you would need to start the command prompt as an administrator. Be careful when deleting folders with administration privileges, the wrong command can very easily break your computer.

Deleting Files Using CMD

The command for deleting files using the cmd is “Del“. Unlike “Rmdir“, “Del“can only delete files. Using “Del” to delete a folder will constitute in an error.
To delete a file simply type “Del” followed by the name of your file along with its extension in quotes.

Del "My File.txt"

Your file should be immediately deleted.
Once again if you file is not located in the users folder or within any of its sub-directories you would need to start the command prompt as an administrator. Be very careful however , the wrong command in an administrator command prompt can very easily break your computer.
To delete more than one file at once simply enter all the names of your files one after the other.

Del "File1.txt" "File2.txt" "File3.txt"

Delete Many Files Or Folders At Once

Entering the name of every file you want to delete is not always practical. Work smarter not harder.
You can delete every file that begins with a set of characters by using a wildcard. Wildcards are special symbols that take the place of characters or words. The most commonly used wildcard is the asterix.
Typing “Del” followed by a set of characters that is shared among the names of the files we want to delete and an asterix, will delete all of them.

Del "File*"

Every file that begins with the word “File” will be deleted.

Delete Files Or Folders From Any Directory

While you can use the “Rmdir” and “Del” commands to delete files and folders in your current directory, what if you want to delete a folder or file that’s located in a different directory without having to navigate there yourself?
You would have to enter your command as usual but instead of typing the name of the file or folder you want to delete directly you would have to enter the location of the target directory followed by a backward slash and then name of your folder or file.
It should look something like this:

Rmdir "C:\Users\Julian\Documents\My Folder".

In the same way you can replace “Rmdir” with any command to complete any task from any directory.

Summary:

  • Navigate to the directory the file you want to delete is located with the “CD” and “Dir” commands.
  • Use “Rmdir” to delete folders and “Del” to delete files. Don’t forget to surround the name of your folder in quotes if it contains a space.
  • Use wildcards to delete many files or folders at once.

Want to learn more about the Command Line?

Available on Udemy with a 70% discount.
Only for readers like you!

Scroll to top