Remove-Item and cmd "RD" intermittently says "The directory is not empty."

We are doomed and haunted with this error message with about 10% of our deployments failing due to a clean up process using either the community step library for cleaning Temporary asp.Net files found here:
https://library.octopusdeploy.com/#!/step-template/actiontemplate-file-system-clean-asp.net-temp-files

The same error message appears when trying to remove a directory with scripts/css files with both strategies below:

Using powershell Remove-Item:

    if (Test-Path $SitecoreScriptsPath) {

      Write-Host "Cleaning up target Script folder $SitecoreScriptsPath"

      Remove-Item -Path $SitecoreScriptsPath -Force -Recurse 
    }

Or using cmd directly:

   if (Test-Path $SitecoreContentPath) {

         Write-Host "Cleaning up target Content folder $SitecoreContentPath"

         \cmd /c "rd $($SitecoreContentPath) /s /q"

   }

The error message we get is:
“The directory is not empty.”

I’m not entirely convinced it has anything to do with the fact we are using an Admin account (instead of the local system account) to run for the Tentacle windows service because it works “most of the time”.

Whoever can pinpoint the reason would be considered god.

Hi

I believe this error comes from the fact that there is a locked file under one of the directories the script tries to remove. These errors are expected as some temp files may be active and are meant to be suppressed. I’ve updated the script to suppress all errors. If you experience any future issues could you provide the verbose PowerShell output as well.

Updated File System - Clean ASP.NET Temp Files script.

Hope that helps!

Daniel

Thanks Daniel

I’ll do something similar to our scripts for it too.

It became a lot more stable and consistent once we found an RDP session to be opened and probably locking that file. Lol.

But even after killing that it hasn’t been 100 percent. So, still need actioning.

Could you confirm if you are still seeing an issue after updating the script?

We actually ended up doing the following which seems to have improved the stability (somewhat):

## Create a C# function to perform the git folder delete correctly
$script = @" 
using System.IO;          

namespace MyNamespace
{
	public static class IOExtensions
	{    
		public static void ForceDeleteDirectory(string path) 
		{
			var directory = new DirectoryInfo(path) { Attributes = FileAttributes.Normal };

			foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
			{
				info.Attributes = FileAttributes.Normal;
			}

			directory.Delete(true);
		}
	}
}
"@

Add-Type -TypeDefinition $script -Language CSharp     


   [Digital.Web.IOExtensions]::ForceDeleteDirectory($PathToDelete);

It’s very bruteforce and you’re welcome to use it. For us, it’s very ideal that our scripts/css are removed from the target directory because we cannot allow them to be picked up particularly because we are using Bundling that includes all items in a folder. So if cleanup fails, we don’t really want it to be very silent.

Issue could be closed.

Cheers.
D

We are having the same issue. We didn’t make any code change nor did we ever have this issue before. We started having this issue when we moved to 3.4.x.

Error: The directory is not empty.

I know for a fact that we put Delete Shares on all the files in this directory it’s complaining about and like I said we haven’t changed code except for moving to 3.4.

We are using the Remove-Item - PS cmd. This happens on Windows 2008R2 and 2012.

As a side note.

We fixed the issue by applying Full Permissions on the Top Directory where the tentacles deploy. The user account we used was the account the Octopus Server uses for it’s service account. That fixed our issue 100%.

I believe this account is used on all the tentacles to do any work on the machines.

Very strange.