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'm trying to execute rd /S on a non-empty directory and sometimes, it works fine, sometimes, it reports "The directory is not empty". Why am I getting this error? Is there a reliable way to remove a folder full of files and subdirectories under Windows? PowerShell's Remove-Item also suffers from this issue.

share|improve this question
    
Have you checked file/directory NTFS permissions? –  paradroid Sep 19 '12 at 8:18
    
Yes. The script is running under admin privileges. If I run it multiple times, it will eventually work fine, like if every error deleted at least some sub-folders and eventually, the rd command would work fine. –  Borek Sep 19 '12 at 8:20
    
Do the directories contain system or hidden files? Try attrib /s -s -h C:\directory first. –  paradroid Sep 19 '12 at 8:40
    
Any running process somewhere around that dir? –  Langhard Sep 19 '12 at 8:46
    
The directory was created by Copy-Item -Recursive a few seconds before rd /S is called on it again. No programs have anything open inside that directory. –  Borek Sep 19 '12 at 8:48

4 Answers 4

up vote 3 down vote accepted

It may be the local anti-virus or indexing service scanning and locking the newly copied files from being instantly deleted.

I would program a delay and retry on failure.

for (i = 1 to 10 etc...)
{
  try to remove directory
  if directory does not exist, break out of loop
  wait a second
}
if directory still exists, abort!
share|improve this answer
    
That was probably it although I can't confirm it. My solution was to use this PowerShell function: pauerschell.blogspot.cz/2010/05/… enhanced with Start-Sleep. Nasty, nasty, nasty but the only solution that worked. –  Borek Sep 19 '12 at 16:14

I'm trying to execute rd /S on a non-empty directory and sometimes, it works fine, sometimes, it reports The directory is not empty. Why am I getting this error?

Because it contains hidden files or files requiring different permissions.

Is there a reliable way to remove a folder full of files and subdirectories under Windows?

Look inside the folder to see what files are left and check if you can manually delete them. If not, you may need different permissions. Try running it from an elevated command-prompt.

PowerShell's Remove-Item also suffers from this issue.

If it’s not a permission issue, you can force it: Remove-Item -Recurse -Force …

share|improve this answer
    
rd /s will remove hidden files as well, /q only enables quiet mode. –  Karan Sep 19 '12 at 15:35
    
I did, before commenting! :) Just created a dir, added files to it, set some to +r, +rh, +h etc., then used rd /s to remove the dir completely after agreeing to the prompt. Worked fine for me on Win7 x64; no idea why it doesn't for you. Edit: @Synetech: Wait, did you remove your comment after confirming that rd /s does indeed remove hidden files? –  Karan Sep 19 '12 at 15:39

Try this:

del c:\test\*.* /Q
rmdir c:\test /Q
share|improve this answer

It is quite likely that the undeleted empty folder contains some hidden files or that someone is still using the folder.

share|improve this answer

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.