Delete Directory from Command Line: Rmdir

By | March 7, 2022

What are your options when you use CMD to delete a directory? You can delete folders and their contents using the command rmdir in this post. Furthermore, each type of folder deletion is also described with an example: empty folders, folders with white-space names, etc.
Delete Directory from Command Line: Rmdir

How to Delete folder from CMD (Command-Line)?

  • Run the rmdir command on the folder.
    rmdir directoryname

Example:
C:>rmdir emptydir
C:>

How to delete a non-empty folder?

If a folder contains some content, it cannot be removed using the simple rmdir command.

C:>rmdir nonemptydir
The directory is not empty.

You can delete both the folder and the contents of a folder by using the /s option. It recursively removes every subfolder.

C:>rmdir /S nonemptydir
nonemptydir, Are you sure (Y/N)? y
C:>

How to force delete a folder without confirmation?

The /Q switch can be used to delete a directory without asking for confirmation.

rmdir /Q /S nonemptydir

The “rd” command may also be substituted for the “rmdir” command. The command is identical in both names. Windows 2000, Server 2003, Vista, Windows 7 & 10 are compatible with this command.

How to delete a directory with white spaces in the name?

As shown in the example below, Rmdir can delete files in folder names that contain whitespace. Double quotes are all that need to be added.

rmdir /Q /S "folder with spaces in the name"

How to delete the contents of a directory but keep the directory?

This procedure is used when we want to delete a directory’s contents but keep its parent directory. Rather than creating it again, we can reuse it instead. It is impossible to delete the parent directory  rmdir /Q /S as it also deletes the current directory. It is rather better to use the following commands.

forfiles /P directory_path /M * /C "cmd /c if @isdir==FALSE del @file"
forfiles /P directory_path /M * /C "cmd /c if @isdir==TRUE rmdir /S /Q @file"

A two-step process is used here. Delete all files with the first command; meanwhile, remove all subdirectories with the second command.

Errors:

Having appropriate access permissions on a directory is necessary before deleting it. Otherwise, “rmdir” will return an error message saying “Access denied”.

You may also like: Javac is not Recognized as Internal Command or External Command

Leave a Reply

Your email address will not be published. Required fields are marked *