We can delete a directory or folder from command line using
rmdir command. See syntax below
C:\>rmdir emptydir C:\> C:\>rmdir nonemptydir The directory is not empty. C:\>rmdir /S nonemptydir nonemptydir, Are you sure (Y/N)? y C:\>
As you can see in the above examples, we need to use /S to delete a non empty directory.
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
You can specify the directory name in quotes. See the example below.
rmdir /Q /S "folder with spaces in the name"
To delete a directory, you should have appropriate access permissions on the directory. Otherwise you would get ‘Access denied’ error.
Delete everything from directory keeping 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.