Delete directory/folder from command line

Do you want to delete a directory/folder from Windows command prompt? This post explains how to use the command rmdir to delete folders and their contents. You can also find examples for each use case of folder deletion – empty folders, non empty folders, folders with white spaced names etc.

Delete folder from CMD

Run the command rmdir on the folder.

rmdir directoryname

Example:

C:>rmdir emptydir
C:>

How to delete a non empty folder

The simple rmdir does not work for folders having some content. Use /s option to delete the folder contents along with the folder.

C:>rmdir nonemptydir
The directory is not empty. 
C:>rmdir /S nonemptydir
nonemptydir, Are you sure (Y/N)? y 
C:>

Delete a folder without confirmation

To delete directory in quiet mode, without being asked for confirmation, we can use /Q switch.

rmdir /Q /S nonemptydir

We can also use ‘rd’ in place of ‘rmdir‘. Both names refer to the same command. This command works on Windows 2000, Windows XP, Server 2003, Vista and Windows 7.

Deleting directory with white spaces in the name

The same commands work but you need to wrap up the folder name in double quotes as shows in the below example.

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

Delete contents of a directory but keep the directory

The usecase here is to delete all the contents of the directory but keep the parent directory so that we do not need to create it again. rmdir /Q /S does not work here as it deletes the parent directory too. Rather the below commands should do the work.

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"

This works in 2 steps – deleting the files with one command and deleting the sub-directories with the other command.

Errors

To delete a directory, you should have appropriate access permissions on the directory. Otherwise you would get ‘Access denied’ error.