≡ Menu

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.

30 comments… add one
  • Brian

    Thanks dude..I was looking for command line way of deleting nonempty folders.. cheers!!!

  • Anonymous

    Good old DOS….something tells me one day the world will be saved by a DOS operation. ; )

  • Anonymous

    cannot delete… access is denied. am trying to delete a directory on a flash drive. cannot access permissions because explorer isn't working. using win7

  • Diman

    Note:
    Don’t forget to leave a “space” in the command line..
    rmdir /Q /S_“folder with spaces in the name” I meant “space”, ait’t “underscore”. And upper case of “S”. Good luck!

  • chetan

    Hi
    can you show me an example of command to delete this path in bat file.
    C:\Program Files\Microsoft Office Communicator

    am not able to do this . some error s

    • admin

      Is this in Windows 7/Vista?.
      If so then you need to take ownership of the files. You can do that with below command from elevated administrator command prompt.
      takeown /F "c:\Program Files\Microsoft Office Communicator" /R /A

      then run the below command to delete this folder.

      rmdir /S "c:\Program Files\Microsoft Office Communicator"

  • Ashwani

    cd program files\microsoft office communicator
    then enter press
    rd /s “filename”
    Then enter

  • Raj

    I am getting the following error “The process cannot access the file because it is being us
    ed by another process.” what should be the case now?

    • teenager

      You should not have the Dir you want to delete open… Close it and then try the command again

  • Tito

    Could you kindly post the command for deleting my folder which is at Drive G. Name of the folder is Recycler. Thanks

  • Andrew

    Thank you so much. I was trying to delete an entire directory of files, about 200k folders and files, and it kept throwing a warning “this folder is shared with other people”. No such warning with this command!

  • Chauhan Nitin

    i followed your instructions but i’m getting error to delete a folder in cmd. ERROR = The system cannot find the the specified…

    http://postimg.org/image/88qxlnlrt/

    • teenager

      You should make “C:” your default Directory, tot do this type “cd.. && cd..” The type the command again but leave the “C:\” and just type the rest of the code.
      It should look like this: C:>rmdir /Q /S Users\Owner\Songs\New

  • Fahd

    i have installed new win 8 n a back up of old win 8 is in c drive with a name windows old n its not being deleted by ANY means . i have tried this one as well but it says on all files access is denied

  • Bose

    I tried the process many times but it says “access is denied”….
    My entire path is “C:\program files\alwil software\avast5” . How should i delete this avast5 folder with all the folders inside it????????????

    • nick

      run cmd.exe as administrator
      make sure avast service is not running

  • wrf

    Every time I try it says the file cant be found

  • somu

    you’re really awesome. Googled a 1000 times just for this line

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

    Thank you so much for your help. This changed my life.

  • Dametew Buta

    Thank you so much for your help. This changed my life.
    please attache more basic command for beginner person for my email
    tnx

  • MAJ

    I need to do that, help me out

    D:\abc\ has files and folders in it

    but by using rd or rmdir

    rmdir d:\abc\ /s /q will delete abc folder also but i don’t want this.

    • Viper

      Try this:

      rmdir /Q /S D:\abc\*

      the asterisk stands for ‘anything’ in or below the named directory.

  • Rahul

    I want to delete subfolders of a folder older than 10 days.I am able to delete files but not folder

  • rajesh

    I wanted to know the bat file programm for deleting the contents (sub folders) of a folder on certain conditions. can anyone help.?

  • Arjun

    How to delete folders created programatically with timestamp attached at the end.

    Like we do for files :
    DEL comp*

    Is there any command for folders:
    RM com*
    or
    RMDIR com*

  • MingYao

    I’m wanting to delete the directory c:\windows.old I followed the instructions above I run CMD as admin, I typed in: takeown /F c:\windows.old /R /A And hit enter key. It did this part properly but Then I typed in: rmdir /Q /S c:\windows.old And after every entry it said Access denied. what is the command line/s i need to type to atlas delete the directory c:\windows.old ? (using windows 10)

    • Faruq Liman

      I got the same problem…….
      please someone help.

  • hemang tankariya

    thank you !!!!!!!
    i m so glad

  • Faruq Liman

    I followed the instructions above I run CMD as admin, I typed in: takeown /F c:\windows.old /R /A And hit enter key. It did this part properly but Then I typed in: rmdir /Q /S c:\windows.old And after every entry it said Access denied. what is the command line/s i need to type to atlas delete the directory c:\windows.old ? (using windows 10.
    Please somebody help………………………

  • takeown /f D:\OrganizationalUnit\Everyone /r /d y
    icacls D:\OrganizationalUnit\Everyone /grant administrators:F /T
    attrib -s -h -r D:\OrganizationalUnit\EVERYONE /s /d
    forfiles /P D:\OrganizationalUnit\Everyone\ -S -M *.* /D -2 /C “cmd /c del /f /q @PATH”
    forfiles /P D:\OrganizationalUnit\Everyone\ /M * /D -2 /C “cmd /c if @isdir==TRUE rmdir /S /Q @file”

  • Siddharth

    Hello,
    I have four folders and I want to keep two of them along with their contents and delete other two folders. How can I do that?
    My Parent folder -> Folder 1 (file1.txt), Folder 2 (file2.txt), Folder 3 (file3.txt), Folder 4 (file4.txt).
    Need to delete or remove 2 folders and keep 2 folder with their content.
    Need command for this.
    Any help will be appreciated.

Leave a Comment