141

I want to have a batch file that will delete all the folders and files in my cache folder for my wireless toolkit.

Currently I have the following:

cd "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS"
del *.db

This will delete all .db files in my RMS directory, however I want to delete every single thing from this directory. How can I do this?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
user69514
  • 24,321
  • 56
  • 146
  • 183
  • 3
    Type `del /?` at the prompt. It'll give you information in how to use the tool. For this case, `del *.* /s` would work **but don't do it in the wrong directory!** – Michael Todd Jul 26 '11 at 21:03
  • 36
    `rmdir /s /q c:\users\tbrollo\j2mewtk\2.5.2\appdb\RMS` – forsvarir Jul 26 '11 at 21:04
  • I would suggest per the up votes to change the correct answer – GregM Oct 02 '14 at 01:35
  • 7
    forsvarir's answer removes the `RMS` directory as well, which may not be what was intended. (In fact it is often what I _don't_ want, because then I lose the original timestamp of the directory and any permissions it had.) See my answer for how to remove all of the contents of a directory but leave the directory itself in place. – Bill_Stewart Oct 19 '15 at 19:08
  • Possible duplicate of [How to delete files/subfolders in a specific directory at command prompt in Windows](http://stackoverflow.com/questions/1965787/how-to-delete-files-subfolders-in-a-specific-directory-at-command-prompt-in-wind) – Steve Chambers Oct 14 '16 at 14:58

14 Answers14

164

Use:

  • Create a batch file

  • Copy the below text into the batch file

    set folder="C:\test"
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
    

It will delete all files and folders.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
GregM
  • 3,334
  • 3
  • 30
  • 47
  • 4
    Awesome, I was looking for something to clean up files and folders for a monthly batch job and this did the trick. – Dave Harding Oct 22 '13 at 15:28
  • 11
    Make sure the directory exists before you do this. – Bill_Stewart Jul 02 '14 at 03:52
  • 2
    If you have 'special' characters in your filenames, first do `chcp 10000` to change encoding to UTF-16 – Demelziraptor Oct 03 '14 at 02:24
  • 4
    Just want to add a note to the comment from Bill_Stewart . The reason you want to make sure that the directory exists is that otherwise, it will clear whatever directory you are currently in. In my case, it was the batch file I had been working on for over 30 minutes. :( Good thing the rest of the folder was under source control. – helios456 Apr 14 '15 at 15:52
  • And for make the folder automatic , use : `%~dp0` instead of defining `%folder%` – SayJeyHi Jan 16 '16 at 21:37
  • some time it's throwing the directory name is invalid C:\path\to\file which is really invalid but it was deleted by this script. How to fix this error? – vee Jan 16 '17 at 15:56
  • So, quick tip - if you try to run this on a network path, even with the "IF EXIST" statement, it will fail to set the folder and a statement will be recorded in the cmd prompt "UNC paths not supporting, defaulting to windows directory" then it will attempt to delete everything in your windows directory. So be careful. – CBRF23 Feb 02 '17 at 17:04
  • cd /D %folder%.. if NOT %errorlevel% == 0 (exit /b 1) – grenix Mar 14 '17 at 15:34
  • 4
    @vee change the loop to `(rmdir "%%i" /s/q 2>NUL || del "%%i" /s/q >NUL ) ` to suppress the error and deletion messages – Carl Walsh Sep 06 '17 at 20:31
  • It didn't delete the folder I set, but all other files and folders present in the same folder as the batch script. Oh, great! – Rasshu Oct 04 '19 at 23:32
  • @Jafar %~dp0 returns folder that the batch file is run in, so using %~dp0 instead of %folder% would delete all files and folders in the location of the batch file! – TheSteven Oct 07 '19 at 22:59
51

del *.* instead of del *.db. That will remove everything.

DontVoteMeDown
  • 19,660
  • 10
  • 65
  • 96
Jon Martin
  • 3,087
  • 5
  • 23
  • 43
34
IF EXIST "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" (
    rmdir "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" /s /q
)

This will delete everything from the folder (and the folder itself).

infojolt
  • 4,786
  • 2
  • 28
  • 72
16

del *.* will only delete files, but not subdirectories. To nuke the contents of a directory, you can use this script:

@echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START

:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE

:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd

:DONE
endlocal

The pushd changes into the directory of which you want to delete the children. Then when rd asks to delete the current directory and all sub directories, the deletion of the sub directories succeed, but the deletion of the current directory fails - because we are in it. This produces an error which 2> NUL swallows. (2 being the error stream).

vll
  • 7,507
  • 1
  • 23
  • 42
Bill_Stewart
  • 18,984
  • 4
  • 42
  • 53
  • 6
    I puzzled over exactly how this worked for a little bit, so as an explanation for others: The pushd changes into the directory of which you want to delete the children. Then when rd asks to delete the current directory and all sub directories, the deletion of the sub directories succeed, but the deletion of the current directory fails - because we are in it. This produces an error which 2> NUL swallows. (2 being the error stream). Very cunning Mr Stewart! – Kinetic Jun 06 '16 at 10:16
  • Oh, and "setlocal enableextensions" turns on the ability to simultaneously push the current directory onto the stack, and change directory. This only seems to be required pre XP, as there and later it defaults to On anyway. – Kinetic Jun 06 '16 at 10:39
  • Can you integrate the information from comments into the answer (comments may disappear at any moment)? ([***Without*** using *"Edit:"*](http://meta.stackexchange.com/a/230693) [*"Update:"* labels](http://meta.stackexchange.com/a/127655).) – Peter Mortensen Sep 17 '19 at 13:24
15

I just put this together from what morty346 posted:

set folder="C:\test"
IF EXIST "%folder%" (
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)

It adds a quick check that the folder defined in the variable exists first, changes directory to the folder, and deletes the contents.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Fantus_Longhorn
  • 151
  • 1
  • 2
  • 3
    So, quick tip - if you try to run this on a network path, even with the "IF EXIST" statement, it will fail to set the folder and a statement will be recorded in the cmd prompt "UNC paths not supporting, defaulting to windows directory" then it will attempt to delete everything in your windows directory. So be careful. – CBRF23 Feb 02 '17 at 17:04
11

You can do this using del and the /S flag (to tell it to recurse all files from all subdirectories):

del /S C:\Path\to\directory\*

The RD command can also be used. Recursively delete quietly without a prompt:

@RD /S /Q %VAR_PATH%

Rmdir (rd)

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
PodTech.io
  • 3,708
  • 32
  • 23
6
set "DIR_TO_DELETE=your_path_to_the_folder"

IF EXIST %DIR_TO_DELETE% (
    FOR /D %%p IN ("%DIR_TO_DELETE%\*.*") DO rmdir "%%p" /S /Q
    del %DIR_TO_DELETE%\*.* /F /Q
)
Cristian Tetic
  • 165
  • 2
  • 2
4

Use

set dir="Your Folder Path Here"
rmdir /s %dir%
mkdir %dir%

This version deletes without asking:

set dir="Your Folder Here"
rmdir /s /q %dir%
mkdir %dir%

Example:

set dir="C:\foo1\foo\foo\foo3"
rmdir /s /q %dir%
mkdir %dir%

This will clear C:\foo1\foo\foo\foo3.

(I would like to mention Abdullah Sabouin's answer. There was a mix up about me copying him. I did not notice his post. I would like to thank you melpomene for pointing out errors!)

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
3

Try the following; it works for me.

I have an application which dumps data in my "C:\tmp" folder, and the following works the best for me. It doesn't even ask Yes or No to delete the data. I have made a schedule for it to run after every 5 minutes

cd "C:\tmp"

del *.* /Q
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
poorvesh
  • 111
  • 1
  • 3
  • 10
    If the folder "C:\tmp" is not present (if you or some other application has deleted that folder) and if you try running this one, won't it delete the data from other folder inside "C:\"? (It happened with me) – Vikram Jun 11 '14 at 21:29
  • 4
    As Vikram notes: 1) This is very dangerous, and 2) it does not remove subdirectories as stated in the question. – Bill_Stewart Oct 19 '15 at 19:10
  • 3
    In addition to the folders thing... you can just do `del /q c:\tmp\*.*`, which is about a million times safer. Not to mention `cd` will only change path, not drive. – Nyerguds Sep 01 '16 at 10:34
  • 1
    This is terrible advice. As others have mentioned you can end up purging so much essential data as a result of this. Flagged for removal I think it's that bad. – RyanfaeScotland Oct 11 '16 at 16:40
3

Better yet, let's say I want to remove everything under the C:\windows\temp folder.

@echo off
rd C:\windows\temp /s /q
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • 13
    That removes the directory `c:\windows\temp`. I don't think that was what the original question was asking. – Bill_Stewart Jun 30 '14 at 03:28
  • Removing the directory seems easiest to me and if I need to re-create it afterward, I can. One issue could be loss of special folder permissions. However, this seems *much* easier than most of the other answers. – madannes Nov 03 '15 at 13:44
  • 1
    Sadly, delete and immediate recreate from batch script seems to give random "access denied" errors, on Win10... – Nyerguds Sep 01 '16 at 10:40
2

You could use robocopy to mirror an empty folder to the folder you are clearing.

robocopy "C:\temp\empty" "C:\temp\target" /E /MIR

It also works if you can't remove or recreate the actual folder.

It does require an existing empty directory.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Aaron
  • 95
  • 10
  • 2
    According to the help of robocopy, `/MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).` - so I'm fairly sure that `/E` is redundant. – Nyerguds Sep 02 '16 at 08:22
0

Just a modified version of GregM's answer:

set folder="C:\test"
cd /D %folder%
if NOT %errorlevel% == 0 (exit /b 1)
echo Entire content of %cd% will be deleted. Press Ctrl-C to abort
pause

REM First the directories /ad option of dir
for /F "delims=" %%i in ('dir /b /ad') do (echo rmdir "%%i" /s/q)

REM Now the files /a-d option of dir
for /F "delims=" %%i in ('dir /b /a-d') do (echo del "%%i" /q)

REM To deactivate simulation mode remove the word 'echo' before 'rmdir' and 'del'.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
grenix
  • 498
  • 5
  • 11
0

You cannot delete everything with either rmdir or del alone:

  • rmdir /s /q does not accept wildcard params. So rmdir /s /q * will error.
  • del /s /f /q will delete all files, but empty subdirectories will remain.

My preferred solution (as I have used in many other batch files) is:

rmdir /s /q . 2>NUL
MultiplyByZer0
  • 4,341
  • 3
  • 27
  • 46
butfly
  • 55
  • 6
-2
@echo off
@color 0A

echo Deleting logs

rmdir /S/Q c:\log\

ping 1.1.1.1 -n 5 -w 1000 > nul

echo Adding log folder back

md c:\log\

You was on the right track. Just add code to add the folder which is deleted back again.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • 1
    I do not recommend this solution because it removes the directory (thus losing its permissions) and recreates it (possibly with different permissions). – Bill_Stewart Sep 05 '18 at 14:41