173

I am making a batch script and part of the script is trying to remove a directory and all of its sub-directories. I am getting an intermittent error about a sub-directory not being empty. I read one article about indexing being the culprit. I disabled WSearch but I eventually got the error again. Here's the command:

rmdir /S /Q "C:\<dir>\"
2
  • 1
    Can I ask some info about the type of files that windows alerts you about? For me, this only occurs with PDF files shortly after I move/copy them. Commented Aug 16, 2019 at 22:42
  • The problem here is not that the folder is not empty... But because it is being used. Commented Aug 13 at 7:39

17 Answers 17

161

I experienced the same issues as Harry Johnston has mentioned. rmdir /s /q would complain that a directory was not empty even though /s is meant to do the emptying for you! I think it's a bug in Windows, personally.

My workaround is to del everything in the directory before deleting the directory itself:

del /f /s /q mydir 1>nul
rmdir /s /q mydir

(The 1>nul hides the standard output of del because otherwise, it lists every single file it deletes.)

Sign up to request clarification or add additional context in comments.

6 Comments

I ran into a scenario where some sub directories within mydir produced the same "not empty" error. So I had to cd into mydir and perform the del on the files in each of those directories as well. Ultimately it worked, but if I'd have had to repeat the process a third time at another sublevel, I'd have felt pretty defeated.
@gfullam As far as I recall, my method should work on recursive subdirectories too, for any depth. That's what del /s does.
Using explorer (probably also mkdir => NO!), adding a folder (and changing the name) finish the job of removal using only the rd /s /q mydir command.
@RajnishCoder Living with Windows is basically a world of workarounds. This particular issue is nothing compared to other problems with Windows 10!
I had this problem when a hidden directory or file was in the directory that I tried to delete. @BoffinBrain's solution solved that.
|
64

I'm familiar with this problem. The simplest workaround is to conditionally repeat the operation. I've never seen it fail twice in a row - unless there actually is an open file or a permissions issue, obviously!

rd /s /q c:\deleteme
if exist c:\deleteme rd /s /q c:\deleteme

8 Comments

holy molly, this make no sense yet it worked. What the #!%#@! microsoft
IF EXIST hides Access is denied and other situations. I have added somthing like DIR c:\deleteme and erros checking before everything.
Having windows explorer open in a subdirectory or otherwise browsing causes this to fail twice in a row. So make sure you ask it a third time (which actually worked)
Actually it has occurred twice in a row for me without anything open or a permissions issue. My record is seven times. Recommend @BoffinbraiN's approach.
I ran only the first command. Then right-clicked the folder > Properties > Uncheck 'Read-only' attribute > Apply. Viola!, the folder finally disappeared.
|
38

I just encountered the same problem and it had to do with some files being lost or corrupted. To correct the issue, just run check disk:

chkdsk /F e:

This can be run from the search windows box or from a cmd prompt. The /F fixes any issues it finds, like recovering the files. Once this finishes running, you can delete the files and folders like normal.

3 Comments

Perfect, I was trying since half hour
This found some hidden files in my folder, which then I was able to delete. Really nasty 😑
A mix of copying a file into the empty folder (@Grisu118) and this did the trick for me !
37

enter the Command Prompt as Admin and run

rmdir /s <FOLDER>

1 Comment

This is no different to what the OP was doing.
13

I had a similar problem, tried to delete an empty folder via windows explorer. Showed me the not empty error, so I thought I try it via admin cmd, but none of the answers here helped.

After I moved a file into the empty folder. I was able to delete the non empty folder

4 Comments

Interesting. Probably not relevant to the OPs scenario, though.
Holy moly, this was literally the only think that worked of all these other solutions!
This worked for me, thank you very much. But I'm still wondering what was causing that issue (I was trying to delete "D:\Program Files\Microsoft SDKs\NuGet Packages" folder from non-system disk).
A mix of this and chkdsk (@jrose) did the trick for me !
6

Im my case i just moved the folder to root directory like so.

move <source directory> c:\

And then ran the command to remove the directory

rmdir c:\<moved directory> /s /q

3 Comments

rmdir c:\<moved directory> /s /q is the correct answer
It is NOT the correct answer. deleting a folder in the root can cause the same messages : folder is not empty.
I know this is an old thread. Just wanted to say this worked for me ...
6

As @gfullam stated in a comment to @BoffinbraiN's answer, the <dir> you are deleting itself might not be the one which contains files: there might be subdirectories in <dir> that get a "The directory is not empty" message and the only solution then would be to recursively iterate over the directories, manually deleting all their containing files... I ended up deciding to use a port of rm from UNIX. rm.exe comes with Git Bash, MinGW, Cygwin, GnuWin32 and others. You just need to have its parent directory in your PATH and then execute as you would in a UNIX system.

Batch script example:

set PATH=C:\cygwin64\bin;%PATH%
rm -rf "C:\<dir>"

Comments

4

one liner:

if exist folder rmdir /Q /S folder

I'm using this in a NPM script like so (Javascript) :

//package.json
  "scripts": {
    "start": "parcel --no-cache",
    "clean": "if exist dist rmdir /Q /S dist",
    "deploy": "npm run clean && parcel build --no-source-maps && firebase deploy"
  },

Comments

3

Windows sometimes is "broken by design", so you need to create an empty folder, and then mirror the "broken folder" with an "empty folder" with backup mode.

robocopy - cmd copy utility

/copyall - copies everything
/mir deletes item if there is no such item in source a.k.a mirrors source with
destination
/b works around premissions shenanigans

Create en empty dir like this:

mkdir empty

overwrite broken folder with empty like this:

robocopy /copyall /mir /b empty broken

and then delete that folder

rd broken /s
rd empty /s

If this does not help, try restarting in "recovery mode with command prompt" by holding shift when clicking restart and trying to run these command again in recovery mode

1 Comment

Yes, I think Robocopy would work in the OPs scenario, because it automatically retries failed operations. You probably want to specify /W:0 to avoid unnecessary delays. You might not want /b because that requires administrative privileges. Recovery mode isn't relevant to this question, because we're talking about a batch script, not someone manually trying to remove a directory.
3

I had "C:\Users\User Name\OneDrive\Fonts", which was mklink'ed ( /D ) to "C:\Windows\Fonts", and I got the same problem. In my case

cd "C:\Users\User Name\OneDrive"

rd /s Fonts

Y (to confirm the action)

helped me. I hope, that it helps you too ;D

1 Comment

Add /q to avoid the need for confirmation when running a batch.
2

What worked for me is the following. I appears like the RMDir command will issue “The directory is not empty” nearly all the time...

:Cleanup_Temporary_Files_and_Folders

Erase /F /S /Q C:\MyDir

RMDir /S /Q C:\MyDir
If  Exist  C:\MyDir  GoTo Cleanup_Temporary_Files_and_Folders

Comments

2

The reason rd /s refuses to delete certain files is most likely due to READONLY file attributes on files in the directory.

The proper way to fix this, is to make sure you reset the attributes on all files first:

attrib -r %directory% /s /d
rd /s %directory%

There could be others such as hidden or system files, so if you want to play it safe:

attrib -h -r -s %directory% /s /d
rd /s %directory%

Comments

2

Open CMD as administrator

chkdsk c: /F /R
  • Press the “Y” key if asked to check your disk the next time your system restarts.

Restart the machine. After that just delete the folder.

Comments

0

I can think of the following possible causes:

  1. there are files or subdirectories which need higher permissions
  2. there are files in use, not only by WSearch, but maybe by your virus scanner or anything else

For 1.) you can try runas /user:Administrator in order to get higher privileges or start the batch file as administrator via context menu. If that doesn't help, maybe even the administrator doesn't have the rights. Then you need to take over the ownership of the directory.

For 2.) download Process Explorer, click Find/Find handle or DLL... or press Ctrl+F, type the name of the directory and find out who uses it. Close the application which uses the directory, if possible.

2 Comments

The machines have the admin account disabled. I am in the admin group but I am no aware of an administrator password. Any other suggestions to get around the permission issue? Someone mentioned calling another program from batch (like vba).
@Mayhem: getting around permissions is quite hard... We would probably need an exploit for a bug in Windows to do that without a password.
0

Similar to Harry Johnston's answer, I loop until it works.

set dirPath=C:\temp\mytest
:removedir
if exist "%dirPath%" (
    rd /s /q "%dirPath%" 
    goto removedir
)

Comments

0

Force delete the directory (if exists)

Delete.bat

set output_path="C:\Temp\MyFolder"
    
if exist %output_path% (
   echo Deleting %output_path%
   attrib -r /s /d %output_path%
   rd /s /q %output_path%
)

Comments

0

I've fixed this before my making sure there wasn't extra whitespace in the name of the directory I was deleting. This is more of a concern when I had the directory name contained within a variable that I was passing to RD. If you're specifying your directly in quotes then this isn't helpful, but I hope that someone like me comes along with the same problem and sees this. RD /S /Q can work, as I noticed the issue started happening when I changed something in my batch script.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.