RD

Remove (or Delete) a Directory.

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.

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:

C:\> RD C:\docs\Jan "C:\Documents\Mar"

Delete a directory with a trailing space

If you try to delete a directory which has a trailing space in the foldername, WIndows explorer will give the error: "Could Not Find This Item".
Escaping the closing quote seems to bypass the normal logic of trimming trailing spaces and allows you to delete the folder:

C:\> RD "this has a trailing space \"

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 sometimes (but not always) 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

To detect a non zero Exit Code from RD 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!

However this is still not reliable, when "RD /s" is used to recursively delete a directory. If it could not delete items in the subdirectory, it will fail to set an errorlevel or error code.

Due to the above it is best to confirm RD's success with a straightforward check of the target directory's non-existence after running the command.

IF NOT EXIST "C:\Documents\Demo" Echo The delete worked!

If is also worth considering the DELTREE approach of deleting all files first and then the folders.

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

Alternatives

The ROBOCOPY command can be used to delete empty sub folders by moving a folder to itself with the /S option that excludes empty folders. For this to work in a single pass, you may need to close Windows File Explorer (file locks may prevent Robocopy from making changes).

ROBOCOPY folder1 folder1 /S /MOVE

RMDIR is a synonym for RD

RD is an internal command.

Examples

Remove 'C:\demo documents\work' and all files and sub folders:

C:\> RD /S "C:\demo documents\work"

Remove 'C:\source_files' but only if it is already empty:

C:\> RD "C:\source_files"

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

Related commands

CIPHER /w - Overwrite data in a directory (secure delete).
MD - Create Directory.
CD - Change Directory.
DEL - Delete selected files from an entire folder tree.
DELTREE - Script to Delete a folder and all subfolders/files.
Delete only empty folders and log results.
FSUTIL reparsepoint delete - Delete an NTFS reparse point.
INUSE - updated file replacement utility (may not preserve file permissions).
How To: Find out if a directory is empty.
Equivalent PowerShell: Remove-Item - Remove an item (rd/ri/rmdir).
Equivalent bash command (Linux): rmdir - Remove folder(s) rm -rf - Delete directory recursively.

(追記) (追記ここまで)
Copyright © 1999-2026 SS64.com
Some rights reserved

AltStyle によって変換されたページ (->オリジナル) /