Q . How to recursively delete directory from command line in windows?

Mike Tyson. asked, Jan 29 ' 2016

In linux I do the same like

rm -r dir_name

But how can I do the same in windows command line.

  • recursive
  • command-prompt
  • delete-directory

2 Answers

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

RD

Delete folder(s)

Syntax
      RD pathname
      RD /S pathname
      RD /S /Q pathname
   
Key
   /S  : Delete all files and subfolders
         in addition to the folder itself.
         Use this to remove an entire folder tree.

   /Q  : Quiet - do not display Y/N confirmation

Place any long pathnames in double quotes.

RD will only delete an empty folder.
RD /Q will silently fail to delete any folders that are not empty.

RD /S will delete subfolders even if they contain files. 

RD does not support wildcards but you can remove multiple folders in one command:

RD C:\docs\Jan C:\docs\Feb "C:\My Documents\Mar"

Exit code / Errorlevel

In normal use RD will fail to return an ERRORLEVEL to the shell, irrespective if the command succeeds or fails the ERRORLEVEL will be left unchanged.

It will however set an Exit Code

Directory deleted successfully = 0
Invalid option = 1 
Directory not found = 2
Access denied = 5
Directory in use = 32
Directory not empty = 145

A workaround to detect a non zero Exit Code from RD is to use conditional execution to run a command if the RD fails, the second command can be anything, an Echo, Goto or CALL statement:

RD NonExistentFolder || Echo This failed!

If you delete folders using PowerShell then a True/False return code ($?) will be set correctly.

RMDIR is a synonym for RD

RD is an internal command.

“Dying is the most embarrassing thing that can happen to you, because someones got to take care of all your details” - Andy Warhol 

Related:

MD - Create folder(s)
DEL - Delete selected files from an entire folder tree
Delete all empty folders - script 
Delrp - Delete a file/directory and NTFS reparse points.(Win 2K ResKit) 
INUSE - updated file replacement utility (may not preserve file permissions)
Powershell: Remove-Item - Remove an item (rd/ri/rmdir) 
Equivalent bash command (Linux):rmdir - Remove folder(s) rm -rf - Delete directory recursively

Leave a Ansewer