Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 2 down vote accepted

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, 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}
share|improve this answer
    
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.) –  Harry Johnston Jul 16 '13 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 '13 at 22:02
    
OT: This is why I hate windows. Everything is so hard. rm -rf /directory/ should be global on all operating systems. –  Christian Jun 4 at 10:42

Try:

rmdir /S your_directory

or:

rmdir /S /Q your_directory 

to skip confirmation messages.

share|improve this answer
    
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. –  Francesco Zanini Jan 17 '13 at 15:41
    
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 '13 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 '13 at 16:06

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.

share|improve this answer

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
share|improve this answer

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

share|improve this answer
    
Ok, I'm looking forward your answer. Thanks. –  Francesco Zanini Jan 17 '13 at 15:42
    
@FrancescoZanini Done –  Canadian Luke Jan 17 '13 at 17:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.