I use Windows.
I want to delete all files and folders in a folder by system call.
I may call like that:
>rd /s /q c:destination
>md c:destination
Do you know an easier way?
rene
40.8k78 gold badges118 silver badges149 bronze badges
asked Oct 1, 2009 at 9:32
2
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:
del /q destination*
for /d %x in (destination*) do @rd /s /q "%x"
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 /q destination*
for /d %%x in (destination*) do @rd /s /q "%%x"
answered Oct 1, 2009 at 9:41
JoeyJoey
339k83 gold badges680 silver badges679 bronze badges
12
del c:destination*.* /s /q
worked for me. I hope that works for you as well.
ra.
1,79814 silver badges15 bronze badges
answered Oct 11, 2012 at 19:45
SeanSean
4634 silver badges2 bronze badges
5
I think the easiest way to do it is:
rmdir /s /q "C:FolderToDelete"
The last «» in the path is the important part.
This deletes the folder itself. To retain, add mkdir "C:FolderToDelete"
to your script.
AlainD
4,8563 gold badges37 silver badges90 bronze badges
answered Nov 20, 2014 at 10:26
BananBanan
4334 silver badges2 bronze badges
6
Yes! Use Powershell:
powershell -Command "Remove-Item 'c:destination*' -Recurse -Force"
answered Feb 16, 2017 at 15:00
Rosberg LinharesRosberg Linhares
3,3891 gold badge31 silver badges33 bronze badges
1
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.
set targetdir=c:example
del /q %targetdir%*
for /d %%x in (%targetdir%*) do @rd /s /q ^"%%x^"
answered Jan 24, 2014 at 10:05
fractorfractor
1,4942 gold badges14 silver badges30 bronze badges
To delete file:
del PATH_TO_FILE
To delete folder with all files in it:
rmdir /s /q PATH_TO_FOLDER
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:
del /q PATH_TO_FOLDER*.*
for /d %i in (PATH_TO_FOLDER*.*) do @rmdir /s /q "%i"
You can create a script to delete whatever you want (folder or file) like this mydel.bat
:
@echo off
setlocal enableextensions
if "%~1"=="" (
echo Usage: %0 path
exit /b 1
)
:: check whether it is folder or file
set ISDIR=0
set ATTR=%~a1
set DIRATTR=%ATTR:~0,1%
if /i "%DIRATTR%"=="d" set ISDIR=1
:: Delete folder or file
if %ISDIR%==1 (rmdir /s /q "%~1") else (del "%~1")
exit /b %ERRORLEVEL%
Few example of usage:
mydel.bat "pathtofolder with spaces"
mydel.bat pathtofile_or_folder
answered Nov 10, 2016 at 17:44
Maxim SuslovMaxim Suslov
4,0111 gold badge30 silver badges29 bronze badges
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.
Also probably want to set a value for the retry switch, /r
, because the default number of retries is 1 million.
robocopy "C:DoNotDelete_UsedByScriptsEmptyFolder" "c:tempMyDirectoryToEmpty" /MIR /r:3
answered May 27, 2014 at 15:43
BateTechBateTech
5,6423 gold badges19 silver badges31 bronze badges
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:
for /d in (*) do rd /s /q "%a" & (
md "%a")
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.
SteveTurczyn
35.5k6 gold badges40 silver badges53 bronze badges
answered Jul 8, 2014 at 19:43
0
answered Jun 20, 2016 at 17:41
NoWarNoWar
35.7k78 gold badges317 silver badges490 bronze badges
1
It takes 2 simple steps. [/q means quiet, /f means forced, /s means subdir]
-
Empty out the directory to remove
del *.* /f/s/q
-
Remove the directory
cd .. rmdir dir_name /q/s
answered May 11, 2020 at 21:51
Jenna LeafJenna Leaf
2,20321 silver badges29 bronze badges
1
try this, this will search all MyFolder under root dir and delete all folders named MyFolder
for /d /r "C:Userstest" %%a in (MyFolder) do if exist "%%a" rmdir /s /q "%%a"
Paul Roub
36.2k27 gold badges82 silver badges89 bronze badges
answered Jul 14, 2020 at 17:57
del .*
This Command delete all files & folders from current navigation in your command line.
answered Nov 14, 2020 at 9:11
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:
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:
To delete a file, use the following command: del "<filename>"
.
For example, to delete Test file.txt
, just run del "Test File.txt"
.
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:
Also, bonus tip – Command Prompt has basic autocompletion. So you could just type in del test
, press the tab key, and Command Prompt will change it to del "Test File.txt"
.
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.
To remove a directory, just use the command rmdir <directory name>
.
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.
You could cd
into the Subfolder directory and remove the file, then come back with cd ..
and run the rmdir Subfolder
command again, but that would get tedious. And just imagine if there were a bunch of other nested files and directories!
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.
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
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.
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
- 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
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:PathItemName", "DriveLetter:PathItemName"
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.
Also see:
Subhan Zafar is an established IT professional with interests in Windows and Server infrastructure testing and research, and is currently working with Itechtics as a research consultant. He has studied Electrical Engineering and is also certified by Huawei (HCNA & HCNP Routing and Switching).
I would like to delete all files and subfolders in a batch file in Windows 7 and keep the top folder. Basically emptying the folder. What’s the command line instruction for that?
asked Aug 9, 2010 at 16:42
Tony_HenrichTony_Henrich
11.2k28 gold badges84 silver badges114 bronze badges
3
You can do this using del
and the /S
flag (to tell it to remove all files from all subdirectories):
del /S C:Pathtodirectory*
Breakthrough
34k10 gold badges104 silver badges149 bronze badges
answered Aug 9, 2010 at 16:46
13
The best Solution:
e.g. i want to delete all files and sub-directories of parent directory lets say «C:UsersDesktopNew folder». The easy way is create batch file of below three commands.
cd C:UsersDesktopNew folder
del * /S /Q
rmdir /S /Q «C:UsersDesktopNew folder»
Here first it will clean all files in all sub-directories and then cleans all empty sub-directories.
Since current working directory is parent directory i.e.»New folder», rmdir command can’t delete this directory itself.
answered Oct 5, 2013 at 4:44
AnnasahebAnnasaheb
3513 silver badges2 bronze badges
6
Navigate to the parent directory:
pushd "Parent Directory"
Delete the sub folders:
rd /s /q . 2>nul
Hashim Aziz
11.2k31 gold badges92 silver badges152 bronze badges
answered Jul 3, 2014 at 12:38
user340956user340956
1911 silver badge2 bronze badges
6
rmdir "c:pathofyourdirectory" /q /s
Don’t forget to use the quotes and for the /q /s
it will delete all the repositories and without prompting.
Excellll
12.5k11 gold badges50 silver badges78 bronze badges
answered Jul 31, 2013 at 18:23
SuperUserSuperUser
871 silver badge1 bronze badge
2
you can use rmdir to delete the files and subfolder, like this:
rmdir /s/q MyFolderPath
However, it is significantly faster, especially when you have a lot of subfolders in your structure to use del before the rmdir, like this:
del /f/s/q MyFolderPath > nul
rmdir /s/q MyFolderPath
answered Apr 27, 2015 at 7:21
2
You can do it quickly and easily by putting these three instructions in your bat file:
mkdir empty_folder
robocopy /mir empty_folder "path_to_directory"
rmdir empty_folder
answered Feb 2, 2017 at 19:20
firebloodfireblood
3252 gold badges5 silver badges13 bronze badges
2
user340956 was painfully close to the solution, but you know what they say about close…
To be clear, rd /s /q c:foobar
deletes the target directory in addition to its contents, but you don’t always want to delete the directory itself, sometimes you just want to delete its contents and leave the directory alone. The deltree
command could do this, but Micrsoft, in its infinite «wisdom» removed the command and didn’t port it to Windows.
Here’s a solution that works without resorting to third-party tools. It’s probably about as simple and efficient as is possible with a command-line script instead of outright writing an actual executable. It doesn’t set any environment variables and it doesn’t use any loops. It’s also as safe as can be, with error-checking everywhere possible, and also as user-friendly as possible, with built-in docs.
dt.bat
(or dt.cmd
for the kids; whatever, I’m old, I use .bat
🤷):
:: dt is a Windows-compatible version of the deltree command
:: Posted to SuperUser by Synetech: https://superuser.com/a/1526232/3279
@echo off
goto start
:start
if ["%~1"]==[""] goto usage
pushd "%~1" 2>nul
if /i not ["%cd%"]==["%~1"] goto wrongdir
rd /s /q "%~1" 2>nul
popd
goto :eof
:usage
echo Delete all of the contents of a directory
echo.
echo ^> %0 DIR
echo.
echo %0 is a substitute for deltree, it recursively deletes the contents
echo (files and folders) of a directory, but not the directory itself
echo.
echo DIR is the directory whose contents are to be deleted
goto :eof
:wrongdir
echo Could not change to the target directory. Invalid directory? Access denied?
goto :eof
Here’s how it works:
- It checks if a command-line argument has been passed, and prints usage information and quits if not.
- It uses
pushd
to save the current directory, then switch to the target directory, redirecting any errors tonul
for a cleaner command-line experience (and cleaner logs). - It checks to see if the current directory is now the same as the target directory, and prints an error message and quits if it is not. This avoids accidentally deleting the contents of the previous directory if the
pushd
command failed (e.g., passing an invalid directory, access-error, etc.)- This check is case-insensitive, so it’s usually safe on Windows, but isn’t for any case-sensitive file-systems like those used by *nix systems, even under Windows.
- It doesn’t work with short-filenames (e.g.
C:UsersBob Bobsonfoobar
won’t be seen as being the same asC:UsersBobBob~1foobar
even if they actually are). It’s a slight inconvenience to have to use the non-short filename, but it’s better safe than sorry, especially since SFNs aren’t completely reliable or always predictable (and may even be disabled altogether).
- It then uses
rd
to delete the target directory and all of its contents, redirecting any errors (which there should be at least one for the directory itself) tonul
. Some notes about this:- Because the target directory is the current directory, the system has an open file-handle to it, and thus it cannot actually delete it, so it remains as is, which is the desired behavior.
- Because it doesn’t try to remove the target directory until after its contents have been removed, it should now be empty (other than anything that also has open file handles).
- Finally, it uses
popd
to return to the previously-current directory and ends the script.
(If you like, you can comment the script with the above descriptions using rem
or ::
.)
answered Feb 17, 2020 at 22:41
SynetechSynetech
67.6k35 gold badges221 silver badges352 bronze badges
2
If you want to delete all files in a folder, including all subfolders and not rely on some error conditions to keep the root folder intact (like I saw in another answer)
you could have a batch file like this:
@echo off
REM Checking for command line parameter
if "%~1"=="" (
echo Parameter required.
exit /b 1
) else (
REM Change directory and keep track of the previous one
pushd "%~1"
if errorlevel 1 (
REM The directory passed from command line is not valid, stop here.
exit /b %errorlevel%
) else (
REM First we delete all files, including the ones in the subdirs, without confirmation
del * /S /Q
REM Then we delete all the empty subdirs that were left behind
for /f %%D IN ('dir /b /s /a:d "%~1"') DO rmdir /S /Q "%%D"
REM Change directory back to the previous one
popd
REM All good.
exit /b 0
)
)
And then you would simply call it with:
empty_my_folder.bat "C:whateverismy folder"
answered Feb 5, 2014 at 16:39
1
To delete file:
del PATH_TO_FILE
To delete folder with all files in it:
rmdir /s /q PATH_TO_FOLDER
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:
del /q PATH_TO_FOLDER*.*
for /d %i in (PATH_TO_FOLDER*.*) do @rmdir /s /q "%i"
You can create a script to delete whatever you want (folder or file) like this mydel.bat
:
@echo off
setlocal enableextensions
if "%~1"=="" (
echo Usage: %0 path
exit /b 1
)
:: check whether it is folder or file
set ISDIR=0
set ATTR=%~a1
set DIRATTR=%ATTR:~0,1%
if /i "%DIRATTR%"=="d" set ISDIR=1
:: Delete folder or file
if %ISDIR%==1 (rmdir /s /q "%~1") else (del "%~1")
exit /b %ERRORLEVEL%
Few example of usage:
mydel.bat "pathtofolder with spaces"
mydel.bat pathtofile_or_folder
answered Nov 10, 2016 at 17:16
To delete all subdirectories and their contents use robocopy. Create an empty directory, for example C:Empty. Let’s say you want to empty C:test which has lots of subdirectories and files and more subdirectories and more files:
robocopy c:empty c:test /purge
then, rd C:test if need be.
answered May 18, 2021 at 9:31
This worked better for me when I had spaces in the folder names.
@echo off
REM ---- Batch file to clean out a folder
REM Checking for command line parameter
if "%~1"=="" (
echo Parameter required.
exit /b 1
) else (
echo ***********************************************************************************
echo *** Deleting all files, including the ones in the subdirs, without confirmation ***
del "%~1*" /S /Q
echo ***********************************************************************************
REM Deleting all the empty subdirs that were left behind
FOR /R "%~1" %%D IN (.) DO (
if "%%D"=="%~1." (
echo *** Cleaning out folder: %~1 ***
) else (
echo Removed folder "%%D"
rmdir /S /Q "%%D"
)
)
REM All good.
exit /b 0
)
answered Feb 13, 2014 at 18:06
1
If you wanted to empty the folder, my take is:
@ECHO OFF
:choice
cls
set /P c=Which directory? [Desktop, Documents, Downloads, Pictures]
if /I "%c%" EQU "Desktop" set Point = "Desktop"
if /I "%c%" EQU "Documents" set Point = "Documents"
if /I "%c%" EQU "Downloads" set Point = "Downloads"
if /I "%c%" EQU "Pictures" set Point = "Pictures"
if /I "%c%" EQU "Videos" set Point = "Videos"
goto choice
set /P d=Which subdirectory? If you are putting multiple. Your's should be like "path/to/folder" (no files!!)
IF NOT EXIST C:Users%USERNAME%%Point%%d% GOTO NOWINDIR
rmdir C:Users%USERNAME%%Point%%d%
mkdir C:Users%USERNAME%%Point%%d%
:NOWINDIR
mkdir C:Users%USERNAME%%Point%%d%
Simple as that!
I hope I helped you out!
I recommend you to take the whole code, if you don’t want to take the whole code, then you can simplify this with.
IF NOT EXIST *path here* GOTO NOWINDIR
rmdir *path here*
mkdir *path here*
:NOWINDIR
mkdir *path here*
EDIT:
rmdir won’t work if it isn’t empty. To fix that.
IF NOT EXIST *path here* GOTO NOWINDIR
del *path here*/* /S /Q (dont copy this, the above prevents the del command from deleting everything in the folder, this is simallar to another answer.)
rmdir *path here*
mkdir *path here*
:NOWINDIR
mkdir *path here*
Not sure if this works but…
sdelete -s -p *path here*/*
answered Jul 30, 2020 at 8:56
1. What the OP asked for
del /f /s /q "C:somePath*.*"
rmdir /s /q "C:somePath"
mkdir "C:somePath"
That will remove all files and folders in and including the directory of "C:somePath"
but remakes the top directory at the end.
2. What most people will want
del /f /s /q "C:somePath*.*"
rmdir /s /q "C:somePath"
That will completely remove "C:somePath"
and all of its contents
If OP has some oddly specific requirement to not touch the top-level directory in any capacity… they should mention that in their question
answered Jan 22, 2021 at 8:03
1
None of the answers already posted here is very good, so I will add my own answer.
Try this:
for /f "delims=" %i in ('dir pathtofolder /s /b /a:-d') do del "%i" /f /q /s
fot /f "delims=" %i in ('dir pathtofolder /s /b /a:d') do rd "%i" /q /s
This should do it.
answered Jan 22, 2021 at 14:28
Ξένη ΓήινοςΞένη Γήινος
2,4885 gold badges24 silver badges57 bronze badges
1
Seems everyone is missing the fact that we’re wanting to delete multiple sub folders, but NOT delete the parent folder. We may also no know all the names of the subfolders, and don’t want to do each one individually.
So, thinking outside the box, this is how I solved this issue.
mkdir c:EmptyFolderToBeDeletedSoon
Robocopy /Purge c:EmptyFolderToBeDeletedSoon c:FolderIWantEmpty
rmdir c:EmptyFolderToBeDeletedSoon
Make a temp directory that’s empty.
Use the RoboCopy command with the /Purge switch (/PURGE :: delete dest files/dirs that no longer exist in source.) using the empty folder as the source, and the folder we want empty as the destination.
Delete the empty temp folder we created to be the empty source for Robocopy.
Now, you have an empty folder of all files and folders, which is what this whole string was about.
answered Aug 15, 2022 at 20:07
This is what worked for me.
- Navigate inside the folder where you want to delete the files.
- Type:
del *
Y
for yes.- Done
Glorfindel
4,0598 gold badges23 silver badges37 bronze badges
answered May 7, 2018 at 18:43
Example: Delete everything (folders/subfolders/files) in 3D Objects folder but want to leave 3D Objects folder alone
pathThere=»C:UsersPhilosophyPoet3D Objects»
CD pathThere
RMDIR /s /q pathThere
When CMD is oriented to working directory, using RMDIR will delete all folders, subfolders and files from the working directory. Seems like CMD process cannot process itself just like ‘I can’t throw myself into rubbish bin because the rubbish bin need to be seal by someone’
answered Oct 1, 2020 at 13:36
Here’s a two-line solution I just came up with, possibly exploiting a bug or unexpected behavior in robocopy
. This works with the newest version of cmd
and robocopy
on Windows 10 at this writing.
It mirror syncs an empty sub-folder to its parent folder. In other words, it tells the parent folder to have all the same files as the sub-folder: none. Amusingly, this means it also deletes the empty sub-folder that it is instructed to sync with.
This example will empty the Temp
folder for the current user. Note that it is using the %TEMP% environment variable, which cmd
expands to whatever that may be, for example C:UsersDobby_the_FreeAppDataLocalTemp
:
mkdir %TEMP%i_like_cheez
robocopy /mir %TEMP%i_like_cheez %TEMP%
answered Jan 30, 2021 at 1:44
r_alex_hallr_alex_hall
3263 silver badges12 bronze badges
this script works with folders with a space in the name
for /f «tokens=*» %%i in (‘dir /b /s /a:d «%~1″‘) do rd /S /Q «%%~i»
answered Feb 20, 2021 at 22:40
You can delete a folder with subfolders and files using commands, but you need to know the correct tool for the job. On Windows 10, when you have to remove a file or folder with a command terminal, the first tool it comes to mind is the del
command, but you will quickly find out that it won’t work to delete folders with subfolders recursively because the tool only deals with files.
The tool you need to use will depend on the command console if you want to delete folders with content inside of them. If you use Command Prompt, rmdir
(remove directory) is the tool you want to use to delete folders recursively. On the other hand, if you are using PowerShell, Remove-Item
is the cmdlet that will do the job.
This guide will teach you two ways to delete subfolders with Command Prompt and PowerShell on Windows 10.
- Delete folders with subfolders from Command Prompt
- Delete folders with subfolders from PowerShell
Delete folders with subfolders from Command Prompt
To delete a folder with subfolders with a command on Windows 10, use these steps:
-
Open Start on Windows 10.
-
Search for Command Prompt, right-click the top result, and select the Run as administrator option.
-
Type the following command to delete an empty folder and press Enter:
rmdir PATHTOFOLDER-NAME
In the command, replace PATHTOFOLDER-NAME with the folder path and the folder name you want to delete. This example removes the “files” folder:
rmdir C:files
-
Type the following command to delete the folder and subfolders with contents and press Enter:
rmdir /s PATHTOFOLDER-NAME
This example removes the “files” folder, subfolders, and files:
rmdir /s C:files
-
Type the following command to delete a folder with content recursively without a confirmation prompt and press Enter:
rmdir /s /q PATHTOFOLDER-NAME
This example removes the “files” folder, subfolders, and files without prompting for confirmation:
rmdir /s /q C:files
Once you complete the steps, the command will delete the folders with subfolders and files from Windows 10.
The /s
option deletes the folder and its content in the above command, but it prompts confirmation. The /q
option ignores the prompt and deletes the folder recursively.
Delete folders with subfolders from PowerShell
To recursively delete an entire folder with a PowerShell command on Windows 10, use these steps:
-
Open Start.
-
Search for PowerShell, right-click the top result, and select the Run as administrator option.
-
Type the following command to delete an empty folder and press Enter:
Remove-Item PATHTOFOLDER-NAME
In the command, replace PATHTOFOLDER-NAME with the folder path and the folder name you want to delete. This example removes the “files” folder:
Remove-Item C:files
-
Type the following command to delete an empty folder and press Enter:
Remove-Item -Recurse -Force PATHTOFOLDER-NAME
This example removes the “files” folder:
Remove-Item -Recurse -Force C:files
After you complete the steps, the command will delete the folder on Windows 10 and its contents with or without a prompt, depending on the command you choose.
The -Recurse
option tells the command that you want to delete the folder and its contents without prompt confirmation. The -Force
option is not required but allows for erasing special items, including read-only or hidden files.
We may earn commission for purchases using our links to help keep offering the free content. Privacy policy info.
All content on this site is provided with no warranties, express or implied. Use any information at your own risk. Always backup of your device and files before making any changes. Privacy policy info.
In some cases, Windows is not allowing some users to delete their files or folders. You can use the command prompt to delete a file or a folder if you encounter such a problem. In this article, we will show you how exactly you can use this method to delete files and folders.
- How to delete files on Windows 10 with CMD
- How to delete folders on Windows 10 with CMD
- Force Uninstall
Deleting files on Windows 10 with command prompt
The built-in del command can help you delete files on Windows 10. You have to point out the specific path to this file on your PC.
To use this method do the following:
- Open the Start Menu and in the search bar type “cmd”. Right-click on the result and select “Run as Administrator”
- Type in the field the following command where PATH will be replaced with the full path to the file you want to delete.
del path
- After that press Enter
Lets’ see this example to clarify the whole process:
You want to delete a file called Info.txt that is located on your desktop. Use the following command in the Command Prompt where you replace username with your own username:
Del “C:UsersusernameDesktopInfo.txt”
After you enter this command the file will be deleted from the Desktop.
There are several commands that you can use to modify a bit the del command.
For instance, you can add the /p parameter to the command to get Command Prompt to be asked for confirmation before deleting a file.
You can also add the /f parameter to force delete a read-only file.
You can also use Revo Uninstallers’ force uninstall feature if you have trouble removing stubborn programs.
To delete folders in Windows 10 with CMD you have to use the rmdir command
This command will remove all folders including the subfolders and files in them.
To use this command do the following:
- Open the Start Menu and in the search bar type “cmd”. Right-click on the result and select “Run as Administrator”
- Type in the field the following command where PATH will be replaced with the full path to the file you want to delete.
rmdir PATH
- Press Enter to finish the process
For example, to delete a folder named “Info” on your desktop, use the following command where you will replace username with your own username:
rmdir “C:UsersusernameDesktopInfo”
After you press Enter the folder named “Info” on your desktop will be deleted.
If you want to delete all the files and subfolders in the folder you want to delete, add the /s parameter. This will ensure that the folder is removed including the subfolders and files in it.
Conclusion
As you’ve noticed, commands to delete files and folders in Windows 10 can be pretty handy if you have trouble deleting them the regular way.
Post Views: 9,291
- Delete a Folder With Its Contents Manually Through the Command Line
- Delete a Folder With Its Contents Using a Batch File
Deleting folders along with their content can be easily done through Windows Explorer. But, if the files or folders are larger, it will be time-consuming; a folder larger than 5GB in size will take at least 5 to 10 minutes to completely erase.
The fastest way to delete files and folders is through the command line. We can either do it manually using the cmd or create a Batch file.
This tutorial will teach you how to delete folders and subfolders using a Batch file.
Delete a Folder With Its Contents Manually Through the Command Line
The rmdir
command is used to delete a directory (folder), the same as the rd
command. It removes the specified folder along with its subfolders and files.
The syntax of the rmdir
command is shown below.
rmdir [/s [/q]] [<drive>:]<path>
Parameters:
[<drive>:]<path>
— specifies the location of the folder./s
— deletes the specified folder and its subfolders and files./q
— specifies Quiet mode. It does not prompt for a confirmation when deleting a folder, and it will only work if/s
is specified.
To delete a folder and its contents manually, open the command prompt through the Start menu or search bar. Copy the command as shown below.
The following code uses the rmdir
command to delete a directory tree.
rmdir /s /q D:TESTFOLDERtest
Change the destination path to the folder location that you want to delete. The folder will be deleted along with its content.
Delete a Folder With Its Contents Using a Batch File
Rather than deleting folders manually using the command line, we have a one-click solution by creating a Batch file to delete the folders automatically. After the Batch file is created, you can run the Batch file whenever you need to delete a folder.
A Batch file is used to execute commands in a serial order stored in a plain text file. It is saved with the .bat
extension.
Here, we have a folder named test1
with subfolders and files. In this article, we will use this folder as an example.
Delete a Single Folder With Its Contents
To create a batch file, open Notepad from the Start menu and copy the commands as shown below:
echo Delete folders using a batch file
rmdir /s /q D:TESTFOLDERtest1
Save the file with a .bat
extension.
Now, run the file by double-clicking on it to delete a folder and its content. The above command will delete the specified directory along with its content.
We can also delete multiple folders using a Batch file by adding the paths of the different folders. Here, we have two folders named test1
and test2
, which we will take as an example.
Delete Multiple Folders With Its Contents
Open Notepad and copy the commands shown below:
echo Delete folders using a batch file
rmdir /s /q D:TESTFOLDERtest D:TESTFOLDERtest1
Save the file with a .bat
extension, and run the .bat
file by double-clicking.
So, we discussed how to delete a folder and its content manually using cmd and a Batch file. Using the Task Scheduler application, you can use Batch files to perform repetitive tasks, automate tasks, or even schedule tasks.