67

I need to delete one folder containing other folders and files inside. I tried del and rmdir commands but sometimes they fail with some error lines: [PATH]: The directory isn't empty.

Is there any good alternative?

1
  • I've experienced inconsistency in whether rmdir with appropriate flags removes everything in a directory tree (and/or the tree itself), and the only possible explanation I have is what Harry Johnston says. Feb 14, 2016 at 18:23

8 Answers 8

17

This happens to me a lot with my automated build scripts.

I guess the reason might be some application that has a file open in that directory with "share delete". I.e. the application allows a deletion of the file (which is why I figure the DeleteFile call doesn't fail), but the file will only disappear after said application has closed it's handle.

That means the file might still be there when the rmdir command tries to delete the folder, hence the error message. Soon after that, said application will close it's handle, the file will disappear, and when you inspect the folder to see which file rmdir was talking about it will be empty.

At least that's my theory.

The workaround proposed by Harry Johnston looks good. Only I would insert a pause in between the rmdir commands. Of course Windows has no easily scriptable "pause" command (correction: ancient Windows versions don't, newer have - see comments). But if seconds granularity is enough one can use ping to create a pause:

ping -n {desired_delay_in_seconds + 1} 127.0.0.1 >nul

So in total:

rd /s /q foo
:: retry once
if exist foo (
    :: clear errorlevel
    cmd /c
    :: pause
    ping -n 2 127.0.0.1 >nul
    :: retry
    rd /s /q foo
)
:: retry yet again
if exist foo (
    cmd /c
    ping -n 2 127.0.0.1 >nul
    rd /s /q foo
)
:: give up
if exist foo {panic}
6
  • In my experience, a pause is never necessary, but YMMV. (Of course if there really is another application in play it is preferable to identify it and explicitly wait for it to exit if possible.) Jul 16, 2013 at 21:11
  • Well... I implemented a 10x retry-loop with one second delay per retry. Works now. I'm not going to try to remove the pause, only to see it fail again :-) Of course that doesn't mean it's necessary. Just being extra-cautious.
    – Paul Groke
    Jul 16, 2013 at 22:02
  • 2
    OT: This is why I hate windows. Everything is so hard. rm -rf /directory/ should be global on all operating systems.
    – user149961
    Jun 4, 2014 at 10:42
  • 2
    There is actually a "pause" command of sorts; to pause three seconds you can use this: timeout /t 3 Feb 14, 2016 at 18:19
  • @r_alex_hall Indeed (but natively only from Vista onwards, XP and earlier don't have the pause command).
    – misha256
    Jun 23, 2016 at 18:58
71

Try:

rmdir /S your_directory

or:

rmdir /S /Q your_directory 

to skip confirmation messages.

6
  • 1
    I already tried these options but as I wrote del and rmdir doesn't ask anything, they just send out some lines like the one in the description. Jan 17, 2013 at 15:41
  • 3
    rmdir /s will delete anything that's possible to delete. You may have files that are locked by a program, read-only files or files that you need administrative access to delete. There isn't a single command that would take care of all those situations for you
    – nvuono
    Jan 17, 2013 at 15:43
  • If you still get error messages, try with PowerShell: the cmdlet Remove-Item it's a bit powerful than rmdir.
    – AndrewQ
    Jan 17, 2013 at 16:06
  • 2
    rmdir /S <directory> Is deleting directory and subfolers and directories. It also asks Are you sure. On Windows 10. Thanks
    – Jasmeet
    Mar 13, 2017 at 19:05
  • 1
    @r_alex_hall RMDIR /s does indeed delete files in directories. But DEL /s will delete only the files and keep the directories, which is why RMDIR is the better option
    – JCH2k
    Jan 4, 2018 at 9:30
12

You may have some readonly files, you can use the del /F option to get rid of them using

     del /S /F your_directory
     rmdir your_directory

You could also have some hidden files and if you are really sure you want to delete them, then you can do this using

     del /S /F /AH your_directory
     rmdir your_directory

If this still fails, then either you do not have permission to delete some files, or some of the files are still in use.

1
  • This should be the correct answer!
    – jdhao
    Dec 17, 2017 at 9:37
5

I believe there's a bug in Windows 7 (and perhaps other versions) which sometimes causes this symptom; or it might be a bug in third-party software. (Do you have Symantec Endpoint Protection installed by any chance?)

Anyway, I've run across it fairly often. In most cases, the problem can be worked around by running rd /s /q two or three times in a row.

If this is in a batch file, you can do something like this:

rd /s /q foo
if exist foo rd /s /q foo
if exist foo rd /s /q foo
if exist foo echo Help! & pause
1
  • Agreed. Using rd /s foo - that's without the /q silent option - will delete all the sub folders for you, but you have to answer Y to the prompt, so requires batch files to be attended. Less than ideal I know.
    – Anthony
    Jul 13, 2017 at 9:40
3

Use del on the files inside, then rmdir to remove the folder.

To use the rmdir method to remove all the files as well, use the /S switch before the directory name, and /Q to suppress prompting for deleting. This is the best way to do it, as you don't miss any files whatsoever. Be careful using the /Q switch though, as it will not warn you of System or Hidden file attributes

0
2

If you use node you can use the rimraf dependency like this:

run install: npm install rimraf -g

delete folder: rimraf SourceFolder

This helped me when getting the error:

the source file names are larger than is supported by the file system

2
  • Your are using npm? common pc users don't know about node js and I don't think they usually install it, mostly developers do. Good suggestion but simple "rmdir" command is good developingdaily.com/article/technology/… Oct 3, 2020 at 9:02
  • @user3559462 yeah you’re probably right. It’s more of just a suggestion if none of the above work for someone. Oct 3, 2020 at 15:12
1

I think you can use it like this:

msg*your file is going to delete 
pause
del/s /q "C:\Users\Rd\Desktop\New folder (2)\" 
rmdir /s /q "C:\Users\Rd\Desktop\New folder (2)\"
mkdir "C:\Users\Rd\Desktop\New folder (2)"
2
  • 1
    Welcome to SU! Why I downvoted? 1) Poor formatting 2) Provides the same essential solution as which was already posted several years ago (always read the posted solutions before posting a new one!) 3) Is not made fit for the question (your solution recreates the deleted folder, which was not asked for).
    – zagrimsan
    Nov 17, 2015 at 9:14
  • I don't think del/s /q was provided anybody else; infact this is the only solution worked for me on command prompt. Providing additional mkdir is not a fault, I suppose. Poor formatting is what one sees. Thanks @Subham Jun 22, 2021 at 21:15
0

Folder older versions of Windows (DOS, Windows 95/98/ME), DELTREE is the equivalent to RM or RMDIR. I use DELTREE on my Windows 7 workstation in batch files just fine though.

Deletes a directory and all the subdirectories and files in it.

To delete one or more files and directories:
DELTREE [/Y] [drive:]path [[drive:]path[...]]

  /Y              Suppresses prompting to confirm you want to delete
                  the subdirectory.
  [drive:]path    Specifies the name of the directory you want to delete.

Note: Use DELTREE cautiously. Every file and subdirectory within the
specified directory will be deleted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .