How to recursively delete directory from command line in windows?

246

69

What is the windows equivalent of rm -r [directory-name]?

Eric Wilson

Posted 2010-08-23T19:29:30.590

Reputation: 6 388

Answers

362

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

Colin Pickard

Posted 2010-08-23T19:29:30.590

Reputation: 6 774

4What does the /s flag stand for? – Mike R – 2015-03-23T15:45:25.660

For some reason there is a problem if you do this in Powershell. So just use Command Line of Windows. – Devid – 2015-03-31T15:00:16.350

3@MikeR, it might stand for "subdirectories" or something. findstr has the same parameter, which makes more sense it its case, so maybe they're just the same for consistency. – Sam – 2015-05-06T23:58:07.407

rmdir /? will give you the full details of the command line arguments - S is "Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree" and the other option is Q which is "Quiet mode, do not ask if ok to remove a directory tree with /S" – Oliver Dungey – 2015-08-24T10:48:43.613

1@FarmBoy, apologies, it would seem my memories go far too far back. I've updated the answer for Windows XP and newer. – Colin Pickard – 2010-08-23T19:36:18.240

11yes deltree is an old DOS command. It was removed in XP and replaced by rmdir /s – heavyd – 2010-08-23T19:39:55.750

1doesn't work in powershell – shinzou – 2017-08-22T17:25:21.520

57

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

Sean

Posted 2010-08-23T19:29:30.590

Reputation: 571

11This is the only solution that will work when your path is more than 250 odd chars long – Calm Storm – 2013-02-12T16:24:47.783

11

You can do the following in PowerShell, if you're on Windows Vista+ :

rm C:\path\to\delete -r -f[orce]

Devesh Khandelwal

Posted 2010-08-23T19:29:30.590

Reputation: 267

6Windows 10 says -f is ambiguous. But you can run rm -r -force <path> – BrunoLM – 2015-11-06T19:21:54.677

6

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)

Vincent Fourmond

Posted 2010-08-23T19:29:30.590

Reputation: 169

3For me, this removes all files recursively, but is not deleting the directories themselves — diris also not removed. – sergiol – 2016-08-18T17:23:31.617

6You should probably stop using XP, it isn't supported anymore ... – Eric Wilson – 2014-05-17T17:20:11.233

2

From CMD Just run RD /s C:\path\to\delete Hit Y to the prompt

/s ensures all the sub directories are deleted as well.

Reference Run help RD from the command line

Rachit Ranjan

Posted 2010-08-23T19:29:30.590

Reputation: 29

2Thanks for your answer, but this information was already in the accepted answer. (rd and rmdir are the same.) Perhaps it would fit as a comment there. – Ben N – 2016-01-22T23:59:16.243

2

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

Paulmann

Posted 2010-08-23T19:29:30.590

Reputation: 21

0

This will delete "my folder" without prompt:

rd /s /q "C:\Users\gourav.g\AppData\Roaming\my folder"

JerryGoyal

Posted 2010-08-23T19:29:30.590

Reputation: 177