Regardless of the reason, these can only be force deleted using command line only. This article explains using cmd to delete folder or file successfully.

Before we begin

Here are some important things for you to understand before we dig into removing files and folders using Command Prompt and Windows PowerShell. These tips will help you understand the terms and some basic rules of the commands that will be used further in the article.

Before we begin How to remove files and folders using Command Prompt Del/Erase command in cmd Rmdir /rd command in cmd Delete multiple files and folders Delete files and folders in any directory Check the existence of file or folder then remove using IF command How to remove files and folders using Windows PowerShell Delete multiple files and folders Delete files and folders in any directory Delete files and folders with complex and long paths using the command line Closing words

The most important thing to remember here is the syntax of the path and file/folder name. When typing file name, notice whether there is a gap (space) in it. For example, if the folder name has no space in it, it can be written as-is. However, if there is a gap in it, it will need to be written within parenthesis (“”). Here is an example: Another thing to remember is that you might see different outcomes while removing folders that are already empty, and folders that have some content in them. Having said that, you will need to use the dedicated options in the command to remove content from within a folder along with the main folder itself. This is called a recursive action. Furthermore, you must also know how to change your working directory when inside a Command Line Interface. Use the command cd to change your directory, followed by the correct syntax. Here are some examples:

Move back a folder:cd ..Move to a folder within the same working directory:cd “NameOfFolder"Move to a precise folder regardless of your current working directory:cd /d “DriveLetter:\Path\FolderName"For example,cd /d “C:\Users\Subhan\Desktop”Change drive:DriveLetter:

One last thing that might come in handy is being able to view what content is available in the current working directory. This is especially helpful so that you type in the correct spelling of the target file or folder. To view the contents of the current working directory in Command Prompt and PowerShell, type in Dir. Now that we have the basic knowledge, let us show you how you can delete files and folders using the command line on a Windows PC. By default, there are 2 command-line interfaces built into Windows 10 – Command Prompt and Windows PowerShell. Both of these are going to be used‌ ‌to‌ ‌delete‌ ‌content‌ ‌from‌ ‌a‌ ‌computer.

How to remove files and folders using Command Prompt

Let us start with the very basic commands and work our way up from there for Command Prompt. We recommend that you use Command Prompt with administrative privileges so that you do not encounter any additional prompts that you already might have.

Del/Erase command in cmd

Del and Erase commands in Command Prompt are aliases of one another. Meaning, both perform the same function regardless of which one you use. These can be used to remove individual items (files) in the current working directory. Remember that it cannot be used to delete the directories (folders) themselves. Use either of the following commands to do so: Tip: Use the Tab button to automatically complete paths and file/folder names. Replace File/FolderName with the name of the item you wish to remove. Here is an example of us removing files from the working directory: If you try to remove items from within a folder, whether empty or not, you will be prompted for a confirmation action, such as the one below: In such a scenario, you will need to enter Y for yes and N for no to confirm. If you select yes, the items directly within the folder will be removed, but the directory (folder) will remain. However, the subdirectories within the folder will not be changed at all. This problem can be resolved by using the /s switch. In order to remove all of the content within the folder and its subdirectories, you will need to add the recursive option in the command (/s). The slash followed by “s” signifies the recursive option. Refer to the example below to fully understand the concept: We will be using the Del command here to recursively remove the text files within the folder “Final folder,” which also has a subdirectory named “Subfolder.” Subfolder also has 2 sample text files that we will be recursively removing with the following command: Here is its output: As you can see in the image above, we had to enter “y” twice – once for each folder. with each confirmation, 2 text files were removed, as we had stated earlier in this example. However, if we use File Explorer, we can still see that both the directories – “Final folder” and “Subfolder” – are still there, but the content inside them is removed. You can also make another tweak to the command so that it is executed silently and you will not be prompted for confirmation. Here is how: The /q illustrates that the action be taken quietly.

Rmdir /rd command in cmd

Similar to Del and Erase, rmdir and rd are also aliases for one another, which means to remove directory. These commands are used to remove the entire directory and subdirectories (recursively) including their contents. Use the command below to do so: The above command will remove the “New folder” only if it is empty. If a folder has subdirectories, you might get the following prompt: In this case, we will need to apply the option for recursive deletion of items as we have done earlier with the Del command. Of course, this can also be performed with the /q option so that you are not prompted with a confirmation.

Delete multiple files and folders

Up until now, we have completed the task of deleting single items per command. Now let’s see how you can remove multiple selective files or folders. Use the command below to do so: For files: For directories: Here is a before and after comparison of the directory where both of the above commands were executed: You can also use an asterisk (*) concatenated with a file type or file name to perform bulk removal of files with the Del command. However, Microsoft has removed the support for the use of asterisks with rmdir so that users do not accidentally remove entire folders. Here is an example of us removing all .txt files from our current working directory:

Delete files and folders in any directory

We are working on removing content within the current working directory. However, you can also use the commands we have discussed till now to remove files and folders from any directory within your computer. Simply put the complete path of the item you want to delete in enclosed parenthesis, and it shall be removed, as in the example below:

Check the existence of file or folder then remove using IF command

We have already discussed that you can view the contents of the working directory by typing in Dir in Command Prompt. However, you can apply an “if” condition in Command Prompt to remove an item if it exists. If it will not, the action would not be taken. Here is how: Replace File/FolderName in both places with the name of the item (and extension if applicable) to be deleted. Here is an example:if exist Desktop (rmdir /s/q Desktop)

How to remove files and folders using Windows PowerShell

The commands in Windows PowerShell to delete and remove content from your PC are very much similar to those of Command Prompt, with a few additional aliases. The overall functionality and logic are the same. We recommend that you launch Windows PowerShell with administrative privileges before proceeding. The main thing to note here is that unlike Command Prompt, all commands can be used for both purposes – removing individual files as well as complete directories. We ask you to be careful while using PowerShell to delete files and folders, as the directory itself is also removed. The good thing is that you do not need to specify recursive action. If a directory has sub-directories, PowerShell will confirm whether you wish to continue with your deletion, which will also include all child objects (subdirectories). Here is a list of all the commands/aliases that can be used in PowerShell to remove an item:

DelRm-dirremove-itemEraseRdRiRm

We tested all of these commands in our working directory and each of them was successful in deleting the folders as well as individual items, as can be seen below: As can be seen above, the syntax of all the aliases is the same. You can use any of the commands below to delete an item using PowerShell:

Delete multiple files and folders

You can also delete multiple selective files and folders just as we did while using Command Prompt. The only difference is that you will need to provide the complete path of each item, even if you are in the same working directory. Use the command below to do so: Remember to append the file type if the item is not a directory (.txt, .png, etc.), as we have done in the example below: You can also use an asterisk (*) concatenated with a file type or file name to perform bulk removal of files with the Del command, as done in Command Prompt. Here is an example: The command shown above will remove all.txt files in the directory “New folder.”

Delete files and folders in any directory

You can also remove an item in a different directory, just like we did in Command Prompt. Simply enter the complete path to the item in PowerShell, as we have done below:

Delete files and folders with complex and long paths using the command line

Sometimes you may encounter an error while trying to delete an item that may suggest that the path is too long, or the item cannot be deleted as it is buried too deep. Here is a neat trick you can apply using both Command Prompt and PowerShell to initially empty the folder, and then remove it using any of the methods above. Use the command below to copy the contents of one folder (which is empty) into a folder that cannot be deleted. This will also make the destination folder empty, hence making it removable. In this scenario, the EmptyFolder is the source folder that we have deliberately kept empty to copy it to the target folder “FolderToRemove.” You will now see that the folder that was previously unremovable is now empty. You can proceed to delete it using any of the methods discussed in this article.

Closing words

The command line is a blessing for Windows users. You can use any of these commands to remove even the most stubborn files and folders on your computer. Let us know which solution worked for you in the comments section down below.