- cmd Delete Folder – How to Remove Files and Folders in Windows
- Kris Koishigawa
- How to open Command Prompt
- How to delete files with the del command
- How to force delete files with the del command
- How to delete folders with the rmdir command
- How to use the /s flag with rmdir
- Kris Koishigawa
- How To Delete Files & Folders – From The Windows Command Line (CMD, DOS)
- Jump Right In:
- Navigating To The File Using CMD.
- Deleting Folders / Directories Using CMD.
- Deleting Files Using CMD.
- Creating a dummy file with a specific size.
- Delete Many Files Or Folders At Once.
- Delete Files Or Folders From Any Directory.
- What We’ll Learn:
- Navigating To The File Using CMD:
- Deleting Folders / Directories Using CMD
- Deleting Files Using CMD
- Delete Files Or Folders From Any Directory (CMD)
- Summary:
- That’s It!
- How to delete all files and folders in a folder by cmd call
- 12 Answers 12
- Rmdir : Delete directory from command line
- Delete folder from CMD
- How to delete a non empty folder
- Force delete a folder without confirmation
- Deleting directory with white spaces in the name
- Delete contents of a directory but keep the directory
- Errors
- How to delete files/subfolders in a specific directory at the command prompt in Windows
- 15 Answers 15
cmd Delete Folder – How to Remove Files and Folders in Windows
Kris Koishigawa
Sometimes it’s just faster to do things with the command line.
In this quick tutorial we’ll go over how to open Command Prompt, some basic commands and flags, and how to delete files and folders in Command Prompt.
If you’re already familiar with basic DOS commands, feel free to skip ahead.
How to open Command Prompt
To open Command Prompt, press the Windows key, and type in «cmd».
Then, click on «Run as Administrator»:
After that, you’ll see a Command Prompt window with administrative privileges:
Screenshot of Command Prompt window
If you can’t open Command Prompt as an administrator, no worries. You can open a normal Command Prompt window by clicking «Open» instead of «Run as Administrator».
The only difference is that you may not be able to delete some protected files, which shouldn’t be a problem in most cases.
How to delete files with the del command
Now that Command Prompt is open, use cd to change directories to where your files are.
I’ve prepared a directory on the desktop called Test Folder. You can use the command tree /f to see a, well, tree, of all the nested files and folders:
There may be a prompt asking if you want to delete the file. If so, type «y» and hit enter.
Note: Any files deleted with the del command cannot be recovered. Be very careful where and how you use this command.
After that, you can run tree /f to confirm that your file was deleted:
How to force delete files with the del command
Sometimes files are marked as read only, and you’ll see the following error when you try to use the del command:
To get around this, use the /f flag to force delete the file. For example, del /f «Read Only Test File.txt» :
How to delete folders with the rmdir command
To delete directories/folders, you’ll need to use the rmdir or rd command. Both commands work the same way, but let’s stick with rmdir since it’s a bit more expressive.
Also, I’ll use the terms directory and folder interchangeably for the rest of the tutorial. «Folder» is a newer term that became popular with early desktop GUIs, but folder and directory basically mean the same thing.
Note: Any directories deleted with the rmdir command cannot be recovered. Be very careful where and how you use this command.
In this case I want to remove a directory named Subfolder, so I’ll use the command rmdir Subfolder :
But, if you remember earlier, Subfolder has a file in it named Nested Test File.
Like with the del command, there’s a helpful flag we can use to make things much faster and easier.
How to use the /s flag with rmdir
To remove a directory, including all nested files and subdirectories, just use the /s flag:
There will probably be a prompt asking if you want to remove that directory. If so, just type «y» and hit enter.
And that’s it! That should be everything you need to know to remove files and folders in the Windows Command Prompt.
All of these commands should work in PowerShell, which is basically Command Prompt version 2.0. Also, PowerShell has a bunch of cool aliases like ls and clear that should feel right at home if you’re familiar with the Mac/Linux command line.
Did these commands help you? Are there any other commands that you find useful? Either way, let me know over on Twitter.
Kris Koishigawa
Read more posts by this author.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
How To Delete Files & Folders – From The Windows Command Line (CMD, DOS)
Jump Right In:
Navigating To The File Using CMD.
Deleting Folders / Directories Using CMD.
Deleting Files Using CMD.
Creating a dummy file with a specific size.
Delete Many Files Or Folders At Once.
Delete Files Or Folders From Any Directory.
What We’ll Learn:
Welcome!
This guide is all about deleting files and folders from the windows command line.
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.
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.
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:
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.
You might be wondering why we surrounded the name of our folder in quotes. The quotes tell the command line that the spaces separating the words do not constitute an additional parameter and that they are simply a part of the name of our 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:
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
To delete a file simply type “Del” followed by the name of your file along with its extension in quotes.
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.
Entering the name of every file or folder 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.
Every file that begins with the word “File” will be deleted.
To delete a set of folders instead, simply replace the Del command with Rmdir.
Similarly you can delete only files of a specific type by specifying its extension before a wildcard. With the following command for example, we can delete every text file in our current directory.
We can even delete every file or folder by using a wildcard, all by itself.
Here is an example using the Rmdir command:
Every folder in your current directory will be deleted along with any contents within it.
Be careful when deleting files and folders, the wrong command can easily delete important files and folders.
Delete Files Or Folders From Any Directory (CMD)
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:
In the same way you can replace “Rmdir” with any command to complete any task from any directory.
Summary:
That’s It!
You now know how to delete files and folders directly from the command prompt.
If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows command Line.
How to delete all files and folders in a folder by cmd call
I want to delete all files and folders in a folder by system call.
I may call like that:
Do you know an easier way?
12 Answers 12
No, I don’t know one.
If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:
This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).
Note that within a batch file you need to double the % within the for loop:
del c:\destination\*.* /s /q worked for me. I hope that works for you as well.
I think the easiest way to do it is:
The last «\» in the path is the important part.
Yes! Use Powershell:
If the subfolder names may contain spaces you need to surround them in escaped quotes. The following example shows this for commands used in a batch file.
To delete folder with all files in it:
To delete all files from specific folder (not deleting folder itself) is a little bit complicated. del /s *.* cannot delete folders, but removes files from all subfolder. So two commands are needed:
You can create a script to delete whatever you want (folder or file) like this mydel.bat :
Few example of usage:
One easy one-line option is to create an empty directory somewhere on your file system, and then use ROBOCOPY (http://technet.microsoft.com/en-us/library/cc733145.aspx) with the /MIR switch to remove all files and subfolders. By default, robocopy does not copy security, so the ACLs in your root folder should remain intact.
I had an index folder with 33 folders that needed all the files and subfolders removed in them. I opened a command line in the index folder and then used these commands:
I separated them into two lines (hit enter after first line, and when asked for more add second line) because if entered on a single line this may not work. This command will erase each directory and then create a new one which is empty, thus removing all files and subflolders in the original directory.
Rmdir : Delete directory from command line
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.
How to delete a non empty folder
The simple rmdir does not work for folders having some content.
Use /s option to delete the folder contents along with the folder. This deletes all subfolders recursively.
Force delete a folder without confirmation
To force delete directory, without being asked for confirmation, we can use /Q switch.
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.
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.
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.
Thanks dude..I was looking for command line way of deleting nonempty folders.. cheers.
Good old DOS….something tells me one day the world will be saved by a DOS operation. ; )
cannot delete… access is denied. am trying to delete a directory on a flash drive. cannot access permissions because explorer isn’t working. using win7
Note:
Don’t forget to leave a “space” in the command line..
rmdir /Q /S_“folder with spaces in the name” I meant “space”, ait’t “underscore”. And upper case of “S”. Good luck!
Hi
can you show me an example of command to delete this path in bat file.
C:\Program Files\Microsoft Office Communicator
Is this in Windows 7/Vista?.
If so then you need to take ownership of the files. You can do that with below command from elevated administrator command prompt.
takeown /F «c:\Program Files\Microsoft Office Communicator» /R /A
then run the below command to delete this folder.
rmdir /S «c:\Program Files\Microsoft Office Communicator»
cd program files\microsoft office communicator
then enter press
rd /s “filename”
Then enter
I am getting the following error “The process cannot access the file because it is being us
ed by another process.” what should be the case now?
You should not have the Dir you want to delete open… Close it and then try the command again
Could you kindly post the command for deleting my folder which is at Drive G. Name of the folder is Recycler. Thanks
Thank you so much. I was trying to delete an entire directory of files, about 200k folders and files, and it kept throwing a warning “this folder is shared with other people”. No such warning with this command!
i followed your instructions but i’m getting error to delete a folder in cmd. ERROR = The system cannot find the the specified…
You should make “C:” your default Directory, tot do this type “cd.. && cd..” The type the command again but leave the “C:\” and just type the rest of the code.
It should look like this: C:>rmdir /Q /S Users\Owner\Songs\New
run cmd.exe as administrator
make sure avast service is not running
Every time I try it says the file cant be found
you’re really awesome. Googled a 1000 times just for this line
“C:\>rmdir /S nonemptydir
nonemptydir, Are you sure (Y/N)? y”
Thank you so much for your help. This changed my life.
Thank you so much for your help. This changed my life.
please attache more basic command for beginner person for my email
tnx
I need to do that, help me out
D:\abc\ has files and folders in it
but by using rd or rmdir
rmdir d:\abc\ /s /q will delete abc folder also but i don’t want this.
the asterisk stands for ‘anything’ in or below the named directory.
I want to delete subfolders of a folder older than 10 days.I am able to delete files but not folder
I wanted to know the bat file programm for deleting the contents (sub folders) of a folder on certain conditions. can anyone help.?
How to delete folders created programatically with timestamp attached at the end.
Like we do for files :
DEL comp*
Is there any command for folders:
RM com*
or
RMDIR com*
I got the same problem…….
please someone help.
Thank you very much! With these commands I could remove all “c:\Windows.old” files and directories.
Super Cool!! Deleted a stubborn non empty directory in no time. Thanks for this guide.
I have several folder: folder1, folder2, folder3
how to delete all “folder” with *
Note: This commands doest’n work:
RM folder*
or
RMDIR folder*
I want to thank you very much for the help on deleting these files. I ended up using the CMD method, but it worked perfectly. Again, thank you.
Thanks. After struggling for months and hunting the web, I was finally able to delete a folder using your advice “Deleting directory with white spaces in the name”
OMG! Thank you! Was trying to delete the remaining empty folders of an old eset nod, but couldn’t. This did the trick (hoping). I’m just wondering if its still in the background somewhere. T.hanks again
in programatically if any changes in the inner files or directory after delete the parent directory doesnot work
eg:
system(“rmdir /s/q \””d:/test/hari\””);——>it works
but after
system(“rmdir /s/q \””d:/test\””);—–>it doesn’t work
Thank you for these still useful tips.
That worked verywell. Thank You
I tried the command, but someone has created the directory repeatedly and now I can not removed the base directory. Help
E:\>rmdir /s /q thur2
thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Th
ur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur
2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\
Thur2\AC_FSQ
1.ZIP – The file name is too long.
thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Th
ur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur
2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\
Thur2\AC_HR1
1.ZIP – The file name is too long.
thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Th
ur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur
2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\
Thur2\AC_HR1
2.ZIP – The file name is too long.
The path thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2
\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\T
hur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thur2\Thu
r2\Thur2\Thur2\Thur2\AC_FSQ
How to delete files/subfolders in a specific directory at the command prompt in Windows
I want to delete every single file and subfolder in this directory, but not the directory itself.
But, there might be an error like ‘this file/folder is already in use’. when that happens, it should just continue and skip that file/folder.
Is there some command for this?
15 Answers 15
You can use this shell script to clean up the folder and files within C:\Temp source:
Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat
rmdir is my all time favorite command for the job. It works for deleting huge files and folders with subfolders. A backup is not created, so make sure that you have copied your files safely before running this command.
This silently removes the folder and all files and subfolders.
The simplest solution I can think of is removing the whole directory with
Then creating this directory again:
This will remove the folders and files and leave the folder behind.
. deletes all files and folders underneath the given directory, but not the directory itself.
. And FOR command line should be modified to for /F «eol=| delims=» %%I in (‘dir «%dir%\*» /AD /B 2^>nul’) do rd /Q /S «%dir%\%%I» because of FOR ignores directories with hidden attribute set. DIR with options /AD /B outputs all directories with just their names. BTW: dir is not a good name for an environment variable. – Mofi Jun 2 ’18 at 8:44
You’ll get an error message, tells you that the RMDIR command can’t access the current folder, thus it can’t delete it.
Update:
From this useful comment (thanks to Moritz Both), you may add && between, so RMDIR won’t run if the CD command fails (e.g. mistyped directory name):
/S: Deletes a directory tree (the specified directory and all its subdirectories, including all files).
/Q: Specifies quiet mode. Does not prompt for confirmation when deleting a directory tree. (Note that /q works only if /s is specified.)
It will remove the contents of the folder, not the folder itself.
RD stands for REMOVE Directory.
/S : Delete all files and subfolders in addition to the folder itself. Use this to remove an entire folder tree.
The simple single command line solution which of course can be also used in a batch file:
This command line contains three commands executed one after the other.
The first command PUSHD pushes current directory path on stack and next makes %PathToFolder% the current directory for running command process.
This works also for UNC paths by default because of command extensions are enabled by default and in this case PUSHD creates a temporary drive letter that points to that specified network resource and then changes the current drive and directory, using the newly defined drive letter.
PUSHD outputs following error message to handle STDERR if the specified directory does not exist at all:
The system cannot find the path specified.
This error message is suppressed by redirecting it with 2>nul to device NUL.
The next command RD is executed only if changing current directory for current command process to specified directory was successful, i.e. the specified directory exists at all.
The command RD with the options /Q and /S removes a directory quietly with all subdirectories even if the specified directory contains files or folders with hidden attribute or with read-only attribute set. The system attribute does never prevent deletion of a file or folder.
Folders used as the current directory for any running process. The entire folder tree to such a folder cannot be deleted if a folder is used as the current directory for any running process.
Files currently opened by any running process with file access permissions set on file open to prevent deletion of the file while opened by the running application/process. Such an opened file prevents also the deletion of entire folder tree to the opened file.
Files/folders on which the current user has not the required (NTFS) permissions to delete the file/folder which prevents also the deletion of the folder tree to this file/folder.
The first reason for not deleting a folder is used by this command line to delete all files and subfolders of the specified folder, but not the folder itself. The folder is made temporarily the current directory for running command process which prevents the deletion of the folder itself. Of course this results in output of an error message by command RD:
The process cannot access the file because it is being used by another process.
File is the wrong term here as in reality the folder is being used by another process, the current command process which executed command RD. Well, in reality a folder is for the file system a special file with file attribute directory which explains this error message. But I don’t want to go too deep into file system management.
This error message, like all other error messages, which could occur because of the three reasons written above, is suppressed by redirecting it with 2>nul from handle STDERR to device NUL.
The third command, POPD, is executed independently of the exit value of command RD.
POPD pops the directory path pushed by PUSHD from the stack and changes the current directory for running the command process to this directory, i.e. restores the initial current directory. POPD deletes the temporary drive letter created by PUSHD in case of a UNC folder path.
For understanding the used commands even better, open a command prompt window, execute there the following commands, and read the help displayed for each command very carefully.
The batch file first makes sure that environment variable PathToFolder is really defined with a folder path without double quotes and without a backslash at the end. The backslash at the end would not be a problem, but double quotes in a folder path could be problematic because of the value of PathToFolder is concatenated with other strings during batch file execution.
Important are the two lines:
The command DEL is used to delete all files in the specified directory.
The commands FOR and RD are used to remove all subdirectories in specified directory. But for /D is not used because of FOR is ignoring in this case subdirectories with the hidden attribute set. For that reason for /F is used to run the following command line in a separate command process started in the background with %ComSpec% /c :
FOR processes the captured output written to handle STDOUT of a started command process which are the names of the subdirectories without path and never enclosed in double quotes.
FOR with option /F ignores empty lines which don’t occur here as DIR with option /B does not output empty lines.
FOR would also ignore lines starting with a semicolon which is the default end of line character. A directory name can start with a semicolon. For that reason eol=| is used to define the vertical bar character as the end-of-line character which no directory or file can have in its name.
Command FOR runs the command RD for each directory name without a path which is the reason why on the RD command line the folder path must be specified once again which is concatenated with the subfolder name.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.