How to solve "The directory is not empty" error when running rmdir command in a batch script?

WindowsBatch File

Windows Problem Overview


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

Windows Solutions


Solution 1 - Windows

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

Solution 2 - Windows

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

Solution 3 - Windows

enter the Command Prompt as Admin and run

rmdir /s <FOLDER>

Solution 4 - Windows

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.

Solution 5 - Windows

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

Solution 6 - Windows

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>"

Solution 7 - Windows

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

Solution 8 - Windows

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

Solution 9 - Windows

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

Solution 10 - Windows

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%

Solution 11 - Windows

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.

Solution 12 - Windows

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
)

Solution 13 - Windows

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

Solution 14 - Windows

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"
  },

Solution 15 - Windows

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.

Solution 16 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMayhemView Question on Stackoverflow
Solution 1 - WindowsBoffinBrainView Answer on Stackoverflow
Solution 2 - WindowsHarry JohnstonView Answer on Stackoverflow
Solution 3 - WindowsAdilson CabralView Answer on Stackoverflow
Solution 4 - WindowsjroseView Answer on Stackoverflow
Solution 5 - WindowsGrisu118View Answer on Stackoverflow
Solution 6 - WindowsGobeView Answer on Stackoverflow
Solution 7 - WindowsMaciej BledkowskiView Answer on Stackoverflow
Solution 8 - WindowsDaniel BardeView Answer on Stackoverflow
Solution 9 - Windowsuser7432246View Answer on Stackoverflow
Solution 10 - WindowsPeter HoegView Answer on Stackoverflow
Solution 11 - WindowsKR93View Answer on Stackoverflow
Solution 12 - WindowsBuvinJView Answer on Stackoverflow
Solution 13 - WindowsAlper EbicogluView Answer on Stackoverflow
Solution 14 - WindowsAlexandre DesrochesView Answer on Stackoverflow
Solution 15 - WindowsThomas WellerView Answer on Stackoverflow
Solution 16 - WindowsKristijonas GrigorovičiusView Answer on Stackoverflow