rd/rmdir - Delete a folder

Maintained on

To delete a folder from the command prompt or a batch file, use the rmdir command (rd for short).

Which command and options you need depends on whether the target is a file or a folder, and whether the folder is empty.

This article explains how to delete a folder, from basic usage and options to troubleshooting when a folder cannot be deleted.

A different command is needed to delete files. See the following article for details.

How to Delete a Folder

To delete a folder, use the rmdir command. Typing rd gives the same result.

The rmdir command is used as follows:

rmdir [/s] [/q] [<Drive>:]<Folder_Path>

The most basic usage is to specify the path of the folder you want to delete after rmdir.

The following example deletes the test folder directly under the current directory.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>rmdir test

After running it, the test folder is deleted.

Without options, rmdir can only delete an empty folder. If the folder contains files or folders, you will get the following error:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>
rmdir test
The directory is not empty.
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

How to delete a non-empty folder is explained in the options section below.

If the path contains spaces, enclose it in double quotation marks, such as rmdir "my folder".

Options for the rmdir Command

The rmdir command has the following options:

OptionDescription
/sDeletes the specified folder itself and all files and subfolders in it
/qDeletes without displaying a confirmation message (used together with /s)

By specifying the /s option, you can delete a folder together with its contents.

When /s is specified, a confirmation message is displayed, and the folder is deleted when you enter Y:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>rmdir /s test
test, Are you sure (Y/N)?

If you do not want to display this message, specify the /q option in addition to /s.

When using rmdir /s in a batch file, the program stops at the confirmation message, so it is recommended to specify the /q option.

チェック

Folders deleted with rmdir /s /q are not moved to the Recycle Bin and cannot be restored. A wrong path can destroy important data, so when building the path from a variable, make sure the value is not empty before running the command.

Examples of Deleting a Folder

Deleting a Temporary Folder in a Batch File

When a batch file creates a temporary folder to do some work, delete it with the rmdir command after the work is done.

@echo off
setlocal

mkdir workspace

rem ...perform operations here...

rmdir /s /q workspace

endlocal

The temporary folder may contain files, so the /s and /q options are specified.

Delete if the Specified Path Is a Folder

To delete the entered path only if it is a folder, write the following:

@echo off
setlocal

set /p target=Please enter the path of the folder you want to delete: 

if exist "%target%\" (
  rmdir /s "%target%"
  echo %target% has been deleted.
) else (
  echo %target% does not exist as a folder.
)

endlocal

Appending \ as in if exist "path\" makes the condition true only when a folder (not a file) exists.

The variable is named target rather than path because assigning to path would overwrite the PATH environment variable used to locate executables.

When you run the code above, you are prompted for the folder to delete and a confirmation message is displayed.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sample.cmd
Please enter the path of the folder you want to delete: test
test, Are you sure (Y/N)? Y
test has been deleted.
C:\users\user>

When a Folder Cannot Be Deleted

The main reasons rmdir fails to delete a folder are as follows:

  • The folder is not empty: specify the /s option.
  • Another application has a file in the folder open: close the application and run the command again.
  • The current directory is the folder you are trying to delete: move to another folder with cd first.
  • You do not have permission: try again in a command prompt run as administrator.

Confirmation Test

Practice Problem

When deleting a folder containing multiple files in a batch file, select the command that will delete the folder and all files in it without stopping subsequent processing.

回答がサーバーに送信されることはありません
#command prompt #batch files #command line #commands #delete folder