I would like to delete all files and subfolders using a batch file in Windows 7 and keep the top folder. Basically emptying the folder.
22 Answers
You can do this using del and the /S flag (to tell it to remove all files from all subdirectories):
del /S C:\Path\to\directory\* - 5Explanation: del stands for Delete and S stands for Recursive.Tamara Wijsman– Tamara Wijsman2010-08-09 16:50:03 +00:00Commented Aug 9, 2010 at 16:50
- 3@Tony_Henrich, Learn more about msdos commands here. computerhope.com/msdos.htmMoab– Moab2010-08-09 18:21:44 +00:00Commented Aug 9, 2010 at 18:21
- 7and using /Q keeps empty sub folders which is undesirable.Tony_Henrich– Tony_Henrich2010-08-09 20:27:23 +00:00Commented Aug 9, 2010 at 20:27
- 29This is the wrong answer and I don't know why it received many up votes. It seems people didn't understand the question fully and don't read comments! I will go through the answers and select the correct one.Tony_Henrich– Tony_Henrich2016-12-04 05:44:24 +00:00Commented Dec 4, 2016 at 5:44
- 8But it leaves the Subfolders how do you also tell it to delete the sub folder itself ?eran otzer– eran otzer2018-02-07 10:19:36 +00:00Commented Feb 7, 2018 at 10:19
The best Solution: e.g. i want to delete all files and sub-directories of parent directory lets say "C:\Users\Desktop\New folder\". The easy way is create batch file of below three commands.
cd C:\Users\Desktop\New folder\
del * /S /Q
rmdir /S /Q "C:\Users\Desktop\New 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.
- 2Works very well, except for a warning when the system cannot delete the root folder.Jerther– Jerther2016-08-25 18:44:57 +00:00Commented Aug 25, 2016 at 18:44
- 3Not good. Hard coded paths and by looking at it, it doesn't empty the folder. It removes it. Very dangerous also. It deletes files at the current folder if the path doesn't exit or mispelled.Tony_Henrich– Tony_Henrich2016-12-04 05:51:01 +00:00Commented Dec 4, 2016 at 5:51
- 1As @Tony_Henrich said the
rmdircommand will deleteNew folderNavigatron– Navigatron2017-04-06 21:18:10 +00:00Commented Apr 6, 2017 at 21:18 - No. This actually works at completely clearing the "New folder" without deleting the folder itself. The only issue is that it gives a minor warning when the OS fails to delete the parent folder. Also, the del command is redundant unless you have a large folder, in which case it might be faster. You could just modify it as such (Sorry about the lack of line breaks in comments): set FOLDER="%userprofile%\Desktop\New folder" cd %FOLDER% rmdir /S /Q %FOLDER%\ >nul 2>&1HSuke– HSuke2017-11-16 18:58:39 +00:00Commented Nov 16, 2017 at 18:58
- 10Real dangerous. If somebody goes ahead and removes/renames `C:\Users\Desktop\New folder`, the very first line with CD fails and your batch file happily deletes everything in the current (default) directory. Which could very well end up being your working directory or C:\Windows\System32Ishmaeel– Ishmaeel2018-01-12 10:18:40 +00:00Commented Jan 12, 2018 at 10:18
Navigate to the parent directory:
pushd "Parent Directory" Delete the sub folders:
rd /s /q . 2>nul - 4Wow, that's hackish. :)Tarnay Kálmán– Tarnay Kálmán2014-07-03 13:08:29 +00:00Commented Jul 3, 2014 at 13:08
- 2This is essentially equivalent to two previous answers.Scott - Слава Україні– Scott - Слава Україні2014-07-03 13:27:09 +00:00Commented Jul 3, 2014 at 13:27
- 2Agreed with above - this is a copy of previous answers, replaced with commands synonymous to those they're replacing.
pushddoesn't add anything here thatcdisn't already doing.Hashim Aziz– Hashim Aziz2018-04-05 05:56:30 +00:00Commented Apr 5, 2018 at 5:56 - 1this is the only one i was able to use so far that worked exactly for what the asker was asking, all the other ones just delete all the files. Of course
rmdir /s path-to-folderwill delete the folder with all the stuff in it, but the asker wanted to know how to delete everything in working directory.Katz_Katz_Katz– Katz_Katz_Katz2018-06-14 09:53:22 +00:00Commented Jun 14, 2018 at 9:53 - 1This answer worked best for me. With all the other answers it didn't delete non-empty directories when using /Q and it always asked for confirmation when not using /Q.marijnr– marijnr2018-10-29 10:31:43 +00:00Commented Oct 29, 2018 at 10:31
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.
- 13The question was "I would like to delete all files and subfolders in a batch file in Windows 7 and keep the top folder."Werner Henze– Werner Henze2014-02-05 17:06:16 +00:00Commented Feb 5, 2014 at 17:06
- This solution works perfectly on windows 10. Thanks.rajakvk– rajakvk2021-08-31 15:19:46 +00:00Commented Aug 31, 2021 at 15:19
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 - This is immensely useful since it also removes any hidden or system files.weaknespase– weaknespase2017-04-04 18:16:42 +00:00Commented Apr 4, 2017 at 18:16
- In Windows even the simplest tasks become complicated beyond description. Add a space before the closing quote if you use filenames with spaces in RobocopyJohannes Linkels– Johannes Linkels2022-08-29 16:18:26 +00:00Commented Aug 29, 2022 at 16:18
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
pushdto save the current directory, then switch to the target directory, redirecting any errors tonulfor 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
pushdcommand 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:\Users\Bob Bobson\foobarwon't be seen as being the same asC:\Users\BobBob~1\foobareven 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
rdto 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
popdto 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 ::.)
- Thank you for this. I created the batch file but it doesn't delete certain folders such as the
Documentsfolder that I backed up fromC:\users\joein a Windows OS. I made that backup usingrobocopyusing the/copyallparameter so I guess it copied security and ownership info. Maybe that stops your batch script from deleting that folder?FlexMcMurphy– FlexMcMurphy2021-05-11 17:15:43 +00:00Commented May 11, 2021 at 17:15 - Yup, that would be my guess. You can try opening an elevated command-prompt and running the batch-file from that.Synetech– Synetech2021-05-12 18:13:01 +00:00Commented May 12, 2021 at 18:13
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 - 1First option gives an error "the directory is not empty". The first command in the second option, deletes the whole folder. It doesn't keep it like I wanted. The second command is not needed if the first command deleted the whole folder.Tony_Henrich– Tony_Henrich2016-12-04 06:02:05 +00:00Commented Dec 4, 2016 at 6:02
- 2The
rmdircommand (both are the same) will delete the parent folder. This is not an answer to the question. Why don't people read?Hashim Aziz– Hashim Aziz2019-10-02 23:00:53 +00:00Commented Oct 2, 2019 at 23:00
1. What the OP asked for
del /f /s /q "C:\some\Path\*.*" rmdir /s /q "C:\some\Path" mkdir "C:\some\Path" That will remove all files and folders in and including the directory of "C:\some\Path" but remakes the top directory at the end.
2. What most people will want
del /f /s /q "C:\some\Path\*.*" rmdir /s /q "C:\some\Path" That will completely remove "C:\some\Path" 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 :)
- It is less "oddly specific" than you think, I just came to that question with this exact requirement (the top folder cannot be removed because the script does not have the permission to do it).Étienne– Étienne2021-08-30 15:27:09 +00:00Commented Aug 30, 2021 at 15:27
- Exactly! This is what I just needed to do to remove Node: del /S /Q c:\users\%username%\AppData\Roaming\npm\* rd /S /Q c:\users\%username%\AppData\Roaming\npm Then, if for some reason you want to leave the top folder in place, just use a third command to create it.Yossi Geretz– Yossi Geretz2024-09-09 02:30:55 +00:00Commented Sep 9, 2024 at 2:30
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 "path\to\folder with spaces" mydel.bat path\to\file_or_folder 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:\whatever\is\my folder" - This left subdirectories in my testingjaycer– jaycer2021-03-11 19:44:25 +00:00Commented Mar 11, 2021 at 19:44
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 path\to\folder /s /b /a:-d') do del "%i" /f /q /s fot /f "delims=" %i in ('dir path\to\folder /s /b /a:d') do rd "%i" /q /s This should do it.
- You don't actually need to do dir /s, because rd /s will delete the subfolders. And you don't need to do dir /s for the files, because you only need to delete files in the root folder. This assumes, of course, that there are no errors when attempting to remove folders.Dan– Dan2022-05-09 18:25:41 +00:00Commented May 9, 2022 at 18:25
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.
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 ) - People always forget about having spaces in the names.Kevin Panko– Kevin Panko2014-02-13 18:48:49 +00:00Commented Feb 13, 2014 at 18:48
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*/* 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.
The way i found to clean all files and subdirectories on a directory is to remove all files recursively with del as others already did, then use a for to remove each subdirectory:
del /s /q "C:\Path\to\directory\*" for /d %a in ("C:\Path\to\directory\*") do rmdir /s /q "%a" Tested on Windows 10, I think it will work on Windows 7.
If used inside a bat file, the % needs to be escaped and '%a' becomes '%%a':
REM Inside a .bat file: del /s /q "C:\Path\to\directory\*" for /d %%a in ("C:\Path\to\directory\*") do rmdir /s /q "%%a" -
del /s /q "C:\Path\to\directory\*"is redundant. For me, on Windows 11 version 23H2, I just usedfor /d %a in ("C:\Path\to\directory\*") do rmdir /s /q "%a"and that was enough to empty the contents of a directory.howdoicode– howdoicode2025-05-23 21:41:59 +00:00Commented May 23 at 21:41 - @howdoicode Reading the help from rmdir on Windows 10 and You are right! But I will try to check on Windows 7 before updating the anwer.Fernando Nascimento– Fernando Nascimento2025-05-24 22:26:14 +00:00Commented May 24 at 22:26
- My fault. I completely overlooked that
for /d %%a in ("C:\Path\to\directory\*") do rmdir /s /q "%%a"only deletes subfolders (and their contents) withinC:\Path\to\directory. You are correct, thatdel /s /q "C:\Path\to\directory\*"is also needed to delete any files outside of any subfolders withinC:\Path\to\directory. By the way, the wildcard*is not necessary in the path when usingdelcommand. For example:del /s /q "C:\Path\to\directory"is enough.howdoicode– howdoicode2025-05-27 17:13:43 +00:00Commented May 27 at 17:13 - Ok! I also overlooked it when read your comment. It is needed indeed! XD The del works as you said (otherwise the rmdir would not be needed), I have intention to test and edit the asnwer but not now. :-)Fernando Nascimento– Fernando Nascimento2025-05-28 20:49:24 +00:00Commented May 28 at 20:49
- No problem. Your answer works fine just the way it is. Thanks for posting your answer as it helped me.howdoicode– howdoicode2025-05-28 21:52:04 +00:00Commented May 28 at 21:52
This is what worked for me.
- Navigate inside the folder where you want to delete the files.
- Type:
del * Yfor yes.- Done
Example: Delete everything (folders/subfolders/files) in 3D Objects folder but want to leave 3D Objects folder alone
pathThere="C:\Users\PhilosophyPoet\3D 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'
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:\Users\Dobby_the_Free\AppData\Local\Temp:
mkdir %TEMP%\i_like_cheez robocopy /mir %TEMP%\i_like_cheez %TEMP% 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"
Assume you are in directory structure
c:\test 123 345 Now, open a commandwindow in c:\test and run this line to remove all (empty or not) subdirectories below:
forfiles /c "cmd /c if @isdir==TRUE rd @file /s /q" How this works: Building upon the copying of files from subdirs ( https://superuser.com/a/1480981/1866142 ) we can use forfiles to handle all files in the current directory. Then, opening we test if the current "file" is a directory. If TRUE, we remove it.
run forfiles /? for more information
Bonus: if there are already files in c:\test they remain inplace and are not touched.
This is essentially equivalent to two previous answers. – Scott - Слава Україні Jul 3, 2014 at 13:27 2 Agreed with above - this is a copy of previous answers, replaced with commands synonymous to those they're replacing. pushd doesn't add anything here that cd isn't already doing. – Hashim Aziz Apr 5, 2018 at 5:56 1 this is the only one i was able to use so far that worked exactly for what the asker was asking, all the other ones just delete all the files. Of course rmdir /s path-to-folder will delete the folder with all the stuff in it, but the asker wanted to know how to delete everything in working directory. – Katz_Katz_Katz Jun 14, 2018 at 9:53 1 This answer worked best for me. With all the other answers it didn't delete non-empty directories when using /Q and it always asked for confirmation when not using /Q
ntvdmbut as 64-bit systems gain market share it's getting increasingly irrelevant.