Batch file. Delete all files and folders in a directory

Batch FileBatch Processing

Batch File Problem Overview


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?

Batch File Solutions


Solution 1 - Batch File

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.

Solution 2 - Batch File

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

Solution 3 - Batch File

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

Solution 4 - Batch File

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

Solution 5 - Batch File

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.

Solution 6 - Batch File

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)

Solution 7 - Batch File

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
)

Solution 8 - Batch File

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

Solution 9 - Batch File

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

Solution 10 - 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

Solution 11 - Batch File

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.

Solution 12 - Batch File

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

Solution 13 - Batch File

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

Solution 14 - Batch File

Easy simple answer :

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

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.

Solution 15 - Batch File

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

Solution 16 - Batch File

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.

Solution 17 - Batch File

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser69514View Question on Stackoverflow
Solution 1 - Batch FileGregMView Answer on Stackoverflow
Solution 2 - Batch FileJon MartinView Answer on Stackoverflow
Solution 3 - Batch FileinfojoltView Answer on Stackoverflow
Solution 4 - Batch FileBill_StewartView Answer on Stackoverflow
Solution 5 - Batch FileFantus_LonghornView Answer on Stackoverflow
Solution 6 - Batch FilePodTech.ioView Answer on Stackoverflow
Solution 7 - Batch FileCristian TeticView Answer on Stackoverflow
Solution 8 - Batch FileTravieDude Minecraft and MoreView Answer on Stackoverflow
Solution 9 - Batch FilepoorveshView Answer on Stackoverflow
Solution 10 - Batch Fileuser3788752View Answer on Stackoverflow
Solution 11 - Batch FileAaronView Answer on Stackoverflow
Solution 12 - Batch FilegrenixView Answer on Stackoverflow
Solution 13 - Batch FilebutflyView Answer on Stackoverflow
Solution 14 - Batch FileAntoine PelletierView Answer on Stackoverflow
Solution 15 - Batch FileIhor DrachukView Answer on Stackoverflow
Solution 16 - Batch FileMichałfView Answer on Stackoverflow
Solution 17 - Batch FileAbdullah SabouniView Answer on Stackoverflow