164

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>\"
1
  • 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

17 Answers 17

153

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.)

6
  • 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
    Commented Feb 10, 2016 at 19:32
  • @gfullam As far as I recall, my method should work on recursive subdirectories too, for any depth. That's what del /s does. Commented Feb 15, 2016 at 23:41
  • 1
    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. Commented Feb 9, 2019 at 0:55
  • 1
    @RajnishCoder Living with Windows is basically a world of workarounds. This particular issue is nothing compared to other problems with Windows 10! Commented Feb 16, 2019 at 15:00
  • I had this problem when a hidden directory or file was in the directory that I tried to delete. @BoffinBrain's solution solved that.
    – Rick
    Commented Jun 24, 2020 at 12:51
63

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
6
  • 21
    holy molly, this make no sense yet it worked. What the #!%#@! microsoft
    – Sam B
    Commented Apr 14, 2017 at 15:15
  • IF EXIST hides Access is denied and other situations. I have added somthing like DIR c:\deleteme and erros checking before everything.
    – it3xl
    Commented Sep 16, 2018 at 11:05
  • 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) Commented Jun 18, 2019 at 5:49
  • 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. Commented Aug 9, 2019 at 23:09
  • I ran only the first command. Then right-clicked the folder > Properties > Uncheck 'Read-only' attribute > Apply. Viola!, the folder finally disappeared. Commented Nov 10, 2020 at 6:39
37

enter the Command Prompt as Admin and run

rmdir /s <FOLDER>
1
  • 20
    This is no different to what the OP was doing. Commented Nov 12, 2018 at 21:22
34

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.

2
  • 2
    Perfect, I was trying since half hour
    – trex
    Commented Jul 2, 2017 at 5:44
  • This found some hidden files in my folder, which then I was able to delete. Really nasty 😑
    – Beat
    Commented Aug 17, 2018 at 11:39
11

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
  • 3
    Please write it in comment.
    – Virb
    Commented Apr 3, 2018 at 11:56
  • Interesting. Probably not relevant to the OPs scenario, though. Commented Nov 12, 2018 at 21:24
  • 2
    Holy moly, this was literally the only think that worked of all these other solutions!
    – Seega
    Commented Aug 26, 2020 at 15:50
  • 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). Commented Apr 29 at 23:46
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
  • 3
    rmdir c:\<moved directory> /s /q is the correct answer
    – lalengua
    Commented Dec 20, 2015 at 23:24
  • 2
    It is NOT the correct answer. deleting a folder in the root can cause the same messages : folder is not empty.
    – Gangnus
    Commented Jan 22, 2016 at 8:26
  • 1
    I know this is an old thread. Just wanted to say this worked for me ... Commented Apr 17, 2018 at 13:21
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>"
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
  • 2
    Add /q to avoid the need for confirmation when running a batch. Commented Mar 26, 2019 at 8:05
3

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"
  },
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
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%
0
2

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
  • 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. Commented Jun 29, 2020 at 19:18
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.

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
  • 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
    Commented Apr 16, 2014 at 13:45
  • @Mayhem: getting around permissions is quite hard... We would probably need an exploit for a bug in Windows to do that without a password. Commented Apr 16, 2014 at 14:22
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
)
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%
)
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.

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