Do you want to delete a directory from Windows command prompt(CMD)? This post explains how to use the command rmdir to delete folders and their contents. You can also find examples for each use case of folder deletion – empty folders, non empty folders, folders with white spaced names etc.
Delete folder from CMD
Run the command rmdir on the folder.
rmdir directoryname
Example:
C:>rmdir emptydir C:>
How to delete a non empty folder
The simple rmdir does not work for folders having some content.
C:>rmdir nonemptydir The directory is not empty.
Use /s option to delete the folder contents along with the folder. This deletes all subfolders recursively.
C:>rmdir /S nonemptydir nonemptydir, Are you sure (Y/N)? y C:>
Force delete a folder without confirmation
To force delete directory, without being asked for confirmation, we can use /Q switch.
rmdir /Q /S nonemptydir
We can also use ‘rd’ in place of ‘rmdir‘. Both names refer to the same command. This command works on Windows 2000, Windows XP, Server 2003, Vista, Windows 7 and 10.
Deleting directory with white spaces in the name
Rmdir can delete files with whitespaces in the name, you just need to wrap up the folder name in double quotes as shown in the below example.
rmdir /Q /S "folder with spaces in the name"
Delete contents of a directory but keep the directory
The usecase here is to delete all the contents of the directory but keep the parent directory so that we do not need to create it again. rmdir /Q /S does not work here as it deletes the parent directory too. Rather the below commands should do the trick.
forfiles /P directory_path /M * /C "cmd /c if @isdir==FALSE del @file" forfiles /P directory_path /M * /C "cmd /c if @isdir==TRUE rmdir /S /Q @file"
This works in 2 steps – the first command deletes all files, whereas the second one deletes all subdirectories.
Errors
To delete a directory, you should have appropriate access permissions on the directory. Otherwise rmdir throws ‘Access denied’ error.
How to delete files recursively with the cmd command prompt in Windows?
Tutorial on how to delete files recursively with cmd, i.e. from a folder and all its subfolders. Use the del batch command line and the recursive option. The Windows delete command provides this useful option automate the deletion process. For example, if you have a huge amount to delete but need to keep the folder and subfolder’s structure.
Advantages of recursivity using Windows cmd
Indeed, it’s the main goal of this tutorial, to be effective and avoid browsing all the subfolders in order to search for a specific file name or extension. The main option that I suggest using is the deletion confirmation, this way you can control one last time which files are recursively deleted. Please note that the files deleted in recursive mode with the command line are not stored in the Windows Recycle Bin. Here is below a list of a few advantages it offers:
- Efficiency: It allows for quick deletion of multiple files and directories with a single command, eliminating the need to select each file or folder individually.
- Precision: With the use of wildcards and specific file extensions, users can target they want to remove, ensuring that only the intended items are deleted without affecting others.
- Depth: It can effortlessly reach and remove files nested deep within multiple subdirectories, which might be tedious through a graphical interface.
- Scripting and Automation: For repetitive tasks, users can create batch scripts to delete specific sets of files, allowing for easy automation and scheduling of clean-up tasks.
- Resource Light: Using cmd is less resource-intensive than graphical interfaces, ensuring smoother system performance, especially when dealing with a large number of files.
- Flexibility: Advanced users can combine file deletion with other command-line operations, achieving complex file management tasks seamlessly.
Make sure you delete the proper files only. The operation is irreversible.
Batch delete files in subdirectories in command prompt
To delete files recursively using the explicit path, without any confirmation prompt, use this command:
del /s "C:\Folder\"
You can also use this variation, without the quotes and without any confirmation prompt. The quotes are mandatory when the complete path of the folder, or the file, contains spaces.
del /s C:\Folder\
Recursively suppress files with a confirmation
This option is the one I recommend. Indeed, make sure to double check and confirm before all files are deleted. Please note that this option deletes only the text files as we use the *.txt filter.
del /p /s SubFolder_1\*.txt
Then a prompt is displayed for the files that match, in alphabetical order:
c:\Folder\SubFolder_1\file (1).txt, Delete (Y/N)?
Delete files and directories without confirmation
To force the deletion with no prompt at all use the delete quiet mode. Useful with a lot of files.
del /q /s SubFolder_2\*.txt
Conclusion and safety tips for batch recursive deletion
To conclude, the del /s recursive option is the fastest and easiest way to delete files recursively with cmd in Windows. Using cmd for recursive file deletion is powerful, but it requires caution. Always double-check paths and file filters to target the correct items. Since files deleted via cmd bypass the Windows Recycle Bin, ensure you have backups of essential data.
When in doubt, use the confirmation prompt (/p option) to verify each file’s deletion. Always prioritize data security when harnessing the command line’s efficiency. Here is how to copy recursively files and folders using the xcopy command.
Some folders and files are impossible to delete using Windows Explorer. These include files with long paths, names or reserved names like CON, AUX, COM1, COM2, COM3, COM4, LPT1, LPT2, LPT3, PRN, NUL etc. You will get an Access Denied error message when you try to delete these files using Windows Explorer, even if you are an administrator.
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.
Table of Contents
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.
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:
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.
Del File/FolderName Erase File/FolderName
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:
Del /s "Final folder"
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:
Del /s /q "Final folder"
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:
rmdir "New Folder"
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.
rmdir /s "Final folder"
Of course, this can also be performed with the /q option so that you are not prompted with a confirmation.
rmdir /s /q "Final folder"
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:
Del "File1.txt" "File3.txt" "File5.txt"
For directories:
rd "Folder1" "Folder3" "Folder5"
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:
Del "*.txt"
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:
if exist File/FolderName (rmdir /s/q File/FolderName)
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:
- Del
- Rm-dir
- remove-item
- Erase
- Rd
- Ri
- Rm
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:
Del File/FolderName Rm-dir File/FolderName remove-item File/FolderName Erase File/FolderName Rd File/FolderName Ri File/FolderName Rm File/FolderName
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:
Del "DriveLetter:\Path\ItemName", "DriveLetter:\Path\ItemName"
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.
robocopy "D:\EmptyFolder" D:\FolderToRemove /MIR
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.
Introduction
If you need to copy an entire directory to a new destination in Windows,
you can use xcopy. This guide will show you how to use xcopy
to recursively copy a directory, all of its subdirectories, and
include hidden files. We will also look at how to remove a directory with
rmdir and move a directory with move.
Recursively copy a directory
The command to recursively copy in Windows command prompt is:
xcopy some_source_dir new_destination_dir\ /E/H
It is important to include the trailing slash \ to tell xcopy the destination
is a directory.
The two options are also important:
/E— Copy all subdirectories/H— Copy hidden files too (e.g..git)
You can see more options by running:
xcopy /?
Move a directory
To simply move a directory instead of copying it, use the move command.
move some_directory new_directory_name
Remove a directory
To remove a directory and all of its contents, you can use rmdir.
rmdir some_directory /Q/S
The important options are:
/Q— Do not prompt with «Are you sure?»/S— Delete all contents and sub-directories
Run rmdir /? for more help.
Conclusion
You should now know how to recursively copy a directory including hidden files
in Windows using xcopy, delete a directory using rmdir, and move a directory
using move.
One of our readers asked us a real interesting question, which we really loved to solve. The question was tricky but the solution was real simple.
The question asked to us was;
Do you know if there’s a way to delete files of a particular filetype from a set of folders quickly in Windows?
For example, I’ve got a folder by the name “ABC” and I’ve got some pictures and music files there, and I need a tool that automatically deletes all those pictures (i.e., files of a particular format like JPEG) quickly from the ABC folder and its sub-folders.
The task of deleting files recursively from folders and sub-folders does not require any tools and can be easily done using the Windows command prompt. For this post we will assume that the directory, you want to delete the files from is D:ABCand it contains several sub-folders from which you want to delete the files.
Note: Before using the command, we want to warn you that the files will be completely deleted bypassing the recycle bin. If you are looking for a safer way to delete files, we can point you to a earlier post where we introduced the recycle command, using which you can safely delete files to the recycle bin using the command prompt, you can also use wildcards to recycle the files.
How to Delete files recursively in folders and sub-folders using the command prompt?
To delete the files, first open a command prompt window and navigate to the folder you want to delete the files from. To do that use the Windows + R hotkey to open the run window, type in cmd without the quotes and hit enter.
Once the command prompt has opened navigate to the folder you want to recursively delete the files from. Once you are in the folder, issue the following command:
del /S test.txt
This command will delete all the files named test.txt inside the current folders, as well as from all the sub-folders present in that folder. The /Sdirective tells the del command to delete all the files recursively from within the folder and sub-folders. See screenshot below.
You can also use wildcards to delete files of only a particular type, the command to delete files recursively based on wildcards will look like:
del /S /P *.jpg
Note: The /P tells the command to prompt the user before deleting the file, we recommend using this with wildcards.
If you want to delete files from only the current directory change the command as below:
del *.jpg
That’s it simple and easy without having to use any additional tools. If you have your own questions don’t forget to ask us. we will always try our best to find a solution for you.
