The Computer Oracle

How to recursively delete directory from command line in windows?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3

--

Chapters
00:00 How To Recursively Delete Directory From Command Line In Windows?
00:13 Accepted Answer Score 525
00:45 Answer 2 Score 81
01:36 Answer 3 Score 5
02:15 Answer 4 Score 10
02:29 Thank you

--

Full question
https://superuser.com/questions/179660/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #commandline #cmdexe

#avk47



ACCEPTED ANSWER

Score 525


deltree if I remember my DOS.


It seems it's been updated... this is what you want:

RMDIR /S

This removes the directory C:\test, with prompts :

rmdir c:\test /s

This does the same, without prompts :

rmdir c:\test /s /q

Regarding the sudo part of your question, if you need more priviliges, you can first open a new shell as another user account using the runas command, like this:

runas /user:Administrator cmd
rmdir c:\test /s /q



ANSWER 2

Score 81


If you want to delete a long and complicated folder structure from the command prompt that RmDir won't touch and not even explorer can display, I've found robocopy can be very efficient at removing the structure. In the example below we have a massive structure inside the folder administrator, the structure is so deep that nothing can remove it. We create a new empty folder called (strangely enough!) "new folder". We then use the robocopy command, telling it the source folder is "new folder" and the destination folder is "D:\Administrator" with the /MIR parameter which means it will purge anything not in the source folder.

robocopy "D:\new folder" D:\Administrator /MIR

In this case the folder paths were so long they would not even fit in the command prompt window Screen Buffer, but Robocopy will traverse the structure and remove any "extra" files and folders (ie anything not in the new empty folder, which is everything).




ANSWER 3

Score 10


For me, what works is

del /s dir

You can add /q to disable confirmation. I've never managed to get rmdir working (on XP)




ANSWER 4

Score 5


If you have a really really long path, (like I did because of java program error), even robocopy cant do it. It descended for about 30sec into my path and then hung.

My solution: if you can move the whole problem path from one folder to another then you can cut away recursivly and repeatedly some directory stairs from the top.

This Batch plays pingpong between the two directories leer and leer2 and cuts away 8 'libraries' each time. If your path contains files, you have to add further commands to erase them.

recurdel.cmd
:loop
move c:\leer\libraries\libraries\libraries\libraries\libraries\libraries\libraries\libraries c:\leer2
rd /S /Q c:\leer\libraries
move c:\leer2\libraries\libraries\libraries\libraries\libraries\libraries\libraries\libraries c:\leer
rd /S /Q c:\leer2\libraries
GOTO loop