40

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?

  • 3
    Not to nitpick, but you're looking for how do do this "from the command prompt" not from "DOS." There hasn't been a DOS subsystem since Win ME. – MDMarra Aug 9 '10 at 18:53
  • @MarkM: Well, there is/was ntvdm but as 64-bit systems gain market share it's getting increasingly irrelevant. – Joey Aug 10 '10 at 9:06
  • See also stackoverflow.com/questions/1965787/… – Vadzim Jan 31 '14 at 19:27

10 Answers 10

51

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\*
  • 2
    yours is better :D +1 - Deleted my answer – BloodPhilia Aug 9 '10 at 16:48
  • 3
    Explanation: del stands for Delete and S stands for Recursive. – Tom Wijsman Aug 9 '10 at 16:50
  • 3
    @Tony_Henrich, Learn more about msdos commands here. computerhope.com/msdos.htm – Moab Aug 9 '10 at 18:21
  • 3
    and using /Q keeps empty sub folders which is undesirable. – Tony_Henrich Aug 9 '10 at 20:27
  • 2
    This 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 Dec 4 '16 at 5:44
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.

  • 1
    Works very well, except for a warning when the system cannot delete the root folder. – Jerther Aug 25 '16 at 18:44
  • 2
    Not 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 Dec 4 '16 at 5:51
  • As @Tony_Henrich said the rmdir command will delete New folder – Nuktu Apr 6 '17 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>&1 – HSuke Nov 16 '17 at 18:58
  • 1
    Real 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\System32 – Ishmaeel Jan 12 '18 at 10:18
7

Navigate to the parent directory:

pushd "Parent Directory"

Delete the sub folders:

rd /s /q . 2>nul
  • 1
    Wow, that's hackish. :) – Tarnay Kálmán Jul 3 '14 at 13:08
  • 1
    This is essentially equivalent to two previous answers. – Scott Jul 3 '14 at 13:27
  • 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 Apr 5 '18 at 5:56
  • 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 '18 at 9:53
  • 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. – marijnr Oct 29 '18 at 10:31
5
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.

  • 2
    The 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 Feb 5 '14 at 17:06
5

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
  • First 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 Dec 4 '16 at 6:02
3

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"
3

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 Apr 4 '17 at 18:16
1

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 Feb 13 '14 at 18:48
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 "path\to\folder with spaces"
mydel.bat path\to\file_or_folder
0

This is what worked for me.

  1. Navigate inside the folder where you want to delete the files.
  2. Type: del *
  3. Y for yes.
  4. Done

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.