rd/rmdir - Delete a folder

Maintained on

When working with command prompt or batch files, it is common to manipulate files and folders on your PC. However, there are different commands to use depending on whether you want to delete a file or a folder, and whether the folder is empty or not.

In this article, we will explain how to delete a folder using the rmdir and rd commands, from basic usage to setting options. Please note that a different command is needed to delete a file.

For more information on deleting files, please refer to our article on the del command.

How to Delete a Folder

To delete a folder, use the rmdir command.

You can also use the rd command instead.

The rmdir command is used as follows:

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

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

The following command is an example of deleting the test folder created directly under the user directory.

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

By executing this command, the test folder will be deleted.

Note that the rmdir command assumes that the folder you want to delete is empty.

We will explain how to delete a non-empty folder later.

Options for the rmdir Command

The rmdir command has the following options:

OptionDescription
/sDeletes files in the specified folder as well
/qDoes not display a confirmation message for deletion

By specifying the /s option, you can also delete files in the specified folder.

When /s is specified, a confirmation message for deletion will be displayed at runtime as follows:

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

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

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

How to Delete a Folder with Examples

Deleting a Temporary Folder in a Batch File

Consider a case where a temporary folder is created in a batch file to perform various operations.

In such a case, you need to delete the temporary folder after the work is done, and you can use the rmdir command for that purpose.

@echo off
setlocal

mkdir workspace

rem ...perform operations here...

rmdir workspace /s /q

endlocal

As there is a possibility that files are included in the temporary folder, the /s and /q options are specified.

Delete if the specified path is a folder

To accept user input in a batch file and delete the input path if it is a folder, you can write the following:

sample.cmd
@echo off
setlocal

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

if exist %path% (
  rmdir %path% /s
  echo %path% has been deleted.
) else (
  echo %path% does not exist.
)

endlocal

If you execute the above code, you will be prompted to input the folder you want to delete and a confirmation message will be displayed before the deletion.

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

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