RD – Windows CMD Command
Remove (or Delete) a Directory.
RD /S pathname
RD /S /Q pathname
/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.
Without the /S option, RD will only delete an empty directory and RD /Q will silently fail to delete any directories that are not empty.
If the pathname is a Junction Point, then RD without /S will remove the Junction point itself, not the Junction’s destination directory.
RD does not support wildcards but you can remove multiple directories in one command:
RD C:\docs\Jan “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, but typically will be an Echo, Goto or CALL statement:
RD NonExistentFolder || Echo This failed!
If you delete directories using PowerShell then a True/False return code ($?) will be set correctly.
RMDIR is a synonym for RD
RD is an internal command.
Remove 'C:\demo documents\work' and all files and sub folders:
RD /S "C:\demo documents\work"
Remove 'C:\source_files' but only if it is already empty: