105

I use Windows.

I want to delete all files and folders in a folder by system call.

I may call like that:

>rd /s /q c:\destination
>md c:\destination

Do you know an easier way?

rene
  • 41,474
  • 78
  • 114
  • 152
ufukgun
  • 6,889
  • 8
  • 33
  • 55
  • 1
    http://superuser.com/questions/173859/how-can-i-delete-all-files-subfolders-in-a-given-folder-via-the-command-prompt – NoWar Jun 20 '16 at 17:37
  • Possible duplicate of [What ever happened to deltree, and what's its replacement?](http://stackoverflow.com/questions/338895/what-ever-happened-to-deltree-and-whats-its-replacement) – Rosberg Linhares Feb 16 '17 at 14:57

12 Answers12

159

No, I don't know one.

If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:

del /q destination\*
for /d %x in (destination\*) do @rd /s /q "%x"

This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).

Note that within a batch file you need to double the % within the for loop:

del /q destination\*
for /d %%x in (destination\*) do @rd /s /q "%%x"
Joey
  • 344,408
  • 85
  • 689
  • 683
  • 3
    i was able to do this without a batch file. use the && to concat the two operations – Matt Broekhuis Oct 19 '12 at 15:58
  • 1
    I am a complete windows-shell noob: how do i set the destination to a path with this example? – low_rents Jan 19 '15 at 11:31
  • You replace the word `destination` by the path, possibly using quotes around the whole thing if necessary. – Joey Jan 19 '15 at 15:24
  • 5
    This answer makes me sad, _because_ it is the best answer – KCD Mar 08 '16 at 21:14
  • The first line is useless. The second will delete both folders and files. – stenci Jun 22 '16 at 15:19
  • @stenci: `rd`, `rmdir`: *Removes (deletes) a directory.* The first line removes all (top-level) files from the directory to empty. The second line will remove all directories (recursively) from the directory to empty. Note that `rd` will not remove files (unless beneath a directory to remove), and `for /d` explicitly only iterates over directories, too. I actually *thought* my answer was clear and straightforward enough, but apparently not. – Joey Jun 22 '16 at 15:26
  • Did you test it? I just tested it: the first line is useless. `rd /s /q "path"` deletes both files and folders – stenci Jun 22 '16 at 16:43
  • 1
    @stenci: The second line can _only_ delete the given directory. It does so recursively, indeed, but it _cannot_ delete a file. Therefore, to remove everything inside a directory you have to remove all files, and all directories. Note that this is _not_ about deleting a single directory and everything within. It's about deleting everything within and _keeping_ the parent directory. – Joey Jun 22 '16 at 19:10
  • You are right. In my case it is useless because the folder contains only sub-folders with sub-sub-folders and files, and I didn't understand the first line because the second one does delete my files. – stenci Jun 23 '16 at 14:03
  • This works but I had to do the `for /d %p in (*) do @rd /s /q "%p"` twice! The first time some directories were reported to be not empty. Windows must be recursing improperly. Perhaps this a flawed multi-thread implementation. – H2ONaCl Jan 16 '17 at 00:48
  • `If you want to retain the original directory for some reason` "For some reason"? You mean like deleting the *contents* of `%temp%` but not the folder itself? Is that a reason? – Synetech Feb 17 '20 at 21:27
  • Beware, don't set the destination as '/' like I just did. This will try to delete C:\... I thought it would delete everything from the current working directory. – user2924019 Apr 24 '20 at 11:48
43

del c:\destination\*.* /s /q worked for me. I hope that works for you as well.

ra.
  • 1,798
  • 14
  • 15
Sean
  • 463
  • 4
  • 2
35

I think the easiest way to do it is:

rmdir /s /q "C:\FolderToDelete\"

The last "" in the path is the important part.

This deletes the folder itself. To retain, add mkdir "C:\FolderToDelete\" to your script.

AlainD
  • 5,413
  • 6
  • 45
  • 99
Banan
  • 433
  • 4
  • 2
18

Yes! Use Powershell:

powershell -Command "Remove-Item 'c:\destination\*' -Recurse -Force"
Rosberg Linhares
  • 3,537
  • 1
  • 32
  • 35
14

If the subfolder names may contain spaces you need to surround them in escaped quotes. The following example shows this for commands used in a batch file.

set targetdir=c:\example
del /q %targetdir%\*
for /d %%x in (%targetdir%\*) do @rd /s /q ^"%%x^"
fractor
  • 1,534
  • 2
  • 15
  • 30
8

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
Maxim Suslov
  • 4,335
  • 1
  • 35
  • 29
1

One easy one-line option is to create an empty directory somewhere on your file system, and then use ROBOCOPY (http://technet.microsoft.com/en-us/library/cc733145.aspx) with the /MIR switch to remove all files and subfolders. By default, robocopy does not copy security, so the ACLs in your root folder should remain intact.

Also probably want to set a value for the retry switch, /r, because the default number of retries is 1 million.

robocopy "C:\DoNotDelete_UsedByScripts\EmptyFolder" "c:\temp\MyDirectoryToEmpty" /MIR /r:3
BateTech
  • 5,780
  • 3
  • 20
  • 31
0

I had an index folder with 33 folders that needed all the files and subfolders removed in them. I opened a command line in the index folder and then used these commands:

for /d in (*) do rd /s /q "%a" & (
md "%a")

I separated them into two lines (hit enter after first line, and when asked for more add second line) because if entered on a single line this may not work. This command will erase each directory and then create a new one which is empty, thus removing all files and subflolders in the original directory.

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
0

Navigate to the parent directory

Line1 pushd "Parent Directory"

Delete the sub folders

Line2 rd /s /q . 2>nul

https://superuser.com/questions/173859/how-can-i-delete-all-files-subfolders-in-a-given-folder-via-the-command-prompt

Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    If you use `pushd` you have to use `popd` too. Unfortunately, `popd` does not work when you delete the directory. So, you have to do `cd` instead of `pushd`. – xmedeko Apr 23 '17 at 17:31
0

It takes 2 simple steps. [/q means quiet, /f means forced, /s means subdir]

  1. Empty out the directory to remove

    del *.* /f/s/q  
    
  2. Remove the directory

    cd ..
    rmdir dir_name /q/s
    

See picture

Jenna Leaf
  • 2,255
  • 21
  • 29
0

try this, this will search all MyFolder under root dir and delete all folders named MyFolder

for /d /r "C:\Users\test" %%a in (MyFolder\) do if exist "%%a" rmdir /s /q "%%a"
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Shailesh Tiwari
  • 295
  • 4
  • 4
-1
del .\*

This Command delete all files & folders from current navigation in your command line.

Yuvraj Hinger
  • 198
  • 1
  • 6