menu

Questions & Answers

Delete all files and folders in a directory

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?

Comments:
2023-01-18 00:00:04
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!
2023-01-18 00:00:04
rmdir /s /q c:\users\tbrollo\j2mewtk\2.5.2\appdb\RMS
2023-01-18 00:00:04
I would suggest per the up votes to change the correct answer
2023-01-18 00:00:04
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.
Answers(17) :

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

Comments:
2023-01-18 00:00:04
Note: If you want to bypass "Are you sure you want to delete..." prompt youll need to add /F Q flags: del . /F /Q
2023-01-18 00:00:04
this only removes files - not folders
2023-01-18 00:00:04
This shouldn't be marked as the correct answer because it doesn't delete folders (as stated in the question).

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
Comments:
2023-01-18 00:00:04
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)
2023-01-18 00:00:04
As Vikram notes: 1) This is very dangerous, and 2) it does not remove subdirectories as stated in the question.
2023-01-18 00:00:04
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.
2023-01-18 00:00:04
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.
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).

Comments:
2023-01-18 00:00:04
I think the question is about emptying the directory of all content, but not deleting the directory itself.

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.

Comments:
2023-01-18 00:00:04
Awesome, I was looking for something to clean up files and folders for a monthly batch job and this did the trick.
2023-01-18 00:00:04
Make sure the directory exists before you do this.
2023-01-18 00:00:04
If you have 'special' characters in your filenames, first do chcp 10000 to change encoding to UTF-16
2023-01-18 00:00:04
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.
2023-01-18 00:00:04
And for make the folder automatic , use : %~dp0 instead of defining %folder%
2023-01-18 00:00:04
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?
2023-01-18 00:00:04
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.
2023-01-18 00:00:04
cd /D %folder%.. if NOT %errorlevel% == 0 (exit /b 1)
2023-01-18 00:00:04
@vee change the loop to (rmdir "%%i" /s/q 2>NUL || del "%%i" /s/q >NUL ) to suppress the error and deletion messages
2023-01-18 00:00:04
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!
2023-01-18 00:00:04
@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!

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

@echo off
rd C:\windows\temp /s /q
Comments:
2023-01-18 00:00:04
That removes the directory c:\windows\temp. I don't think that was what the original question was asking.
2023-01-18 00:00:04
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.
2023-01-18 00:00:04
Sadly, delete and immediate recreate from batch script seems to give random "access denied" errors, on Win10...

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).

Comments:
2023-01-18 00:00:05
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!
2023-01-18 00:00:05
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.
2023-01-18 00:00:05
Can you integrate the information from comments into the answer (comments may disappear at any moment)? (Without using "Edit:" "Update:" labels.)
2023-01-18 00:00:05
This code will fail if the script is executed as: foo.bat jack&jill. Best practice for IF command: if "%~1"=="" goto :HELP. Best practice for using file and folder paths. PUSHD "%~1".
2023-01-18 00:00:05
Actually, your command would execute foo.bat jack followed by jill (because you didn't quote the path). To pass that as a path you would have to quote it anyway (so you don't need "%~1"). Therefore no code change needed.
2023-01-18 00:00:05
You are forgetting one thing. If I drag and drop a folder name onto your batch file that has no spaces and has an ampersand in the path it will not get quoted. Also your code will have problems with the IF comparison if the folder name contains an equals symbol. These are not fringe or obscure cases. Best Practice for coding batch files is to always use quotes with string comparisons and file/folder paths.
2023-01-18 00:00:05
The code is not designed for drag-and-drop from Explorer (if you are using the GUI, you don't need the script anyway). You can add the extra code if you like.

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.

Comments:
2023-01-18 00:00:05
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.

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.

Comments:
2023-01-18 00:00:05
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.

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)

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
)

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'.
@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.

Comments:
2023-01-18 00:00:05
I do not recommend this solution because it removes the directory (thus losing its permissions) and recreates it (possibly with different permissions).

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!)

Comments:
2023-01-18 00:00:05
That doesn't work at all. rmdir foo errors out if foo is not an empty directory.
2023-01-18 00:00:05
@melpomene I found the error and it should be fixed!!
2023-01-18 00:00:05
I didn't mean to copy @Abdullah Sabouni in any way. I must not have noticed this. I'm sorry.
2023-01-18 00:00:05
@melpomene I stated my error and my point of view of what happened. But I have decided not to delete in case it helps in anyway. Thanks for pointing that out.
2023-01-18 00:00:05
@TravieDudeMinecraftandMore +1 - worked for me as well.
2023-01-18 00:00:05
I do not recommend this solution because it removes the directory (thus losing its permissions) and recreates it (possibly with different permissions).

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

Easy simple answer :

C:
del /S /Q C:\folderName\otherFolderName\*

C: Important in case you have to switch from D: to C: or C: to D: (or anything else)

/S Recursive, all subfolders are deleted along

/Q If you don't activate quiet mode, prompt will ask you to type y for every subfolders... you don't want that

Be carful, it's drastic.

Comments:
2023-01-18 00:00:05
This deletes all files recursively, but leaves all subfolders (empty)

I would like to suggest using simple tool like cleardir. So, in batch file you can write:

cleardir path/to/dir

And you'll get empty directory dir. A bit slow, but still resolves the "problem".

I'm an author of the tool =)

The easiest way is:

  1. Create *.txt file
  2. Write: rmdir /q /s . dir
  3. Save file as *.bat in folder which you want to clear (you can call the file NUKE.bat)
  4. Turn it on

WARNING!

THIS DELETES EVERYTHING IN THE FOLDER WHERE IT IS WITHOUT ASKING FOR CONFIRMATION!!!

SO CHOOSE WISELY PLACE FOR SAVING IT.