Cannot remove item. The directory is not empty

PowershellWindows Server-2012-R2

Powershell Problem Overview


I am trying to delete a folder with subfolders/files.

Remove-Item -Force -Recurse -Path $directoryPath

I am getting the error Cannot remove item. The directory is not empty.

My PowershellScript.ps1 has executionPolicy unrestricted. The root folder I try to delete with the current logged in user has full permission on this folder.

On my local pc the code works but not on my Windows Server 2012 R2.

Powershell Solutions


Solution 1 - Powershell

You could try the following:

Remove-Item -Force -Recurse -Path "$directoryPath\*"

Note when using the -Recurse parameter with -Include in Remove-Item, it can be unreliable. So it's best to recurse the files first with Get-ChildItem and then pipe into Remove-Item. This may also help if you deleting large folder structures.

Get-ChildItem $directoryPath -Recurse | Remove-Item -Force   

Solution 2 - Powershell

File is open in another program

I forgot that I had Visual Studio open with my project open and was getting this error.

Close any files associated with that directory, run PowerShell as admin, then run the command:

Remove-Item "C:\path\to\dir" -Recurse -Force

Pro Tip

You can also run this command to open file explorer:

ii "C:\path\to\dir"

If you right click and try to delete it, it might give you a more verbose error than command line.

Solution 3 - Powershell

Note that

Remove-Item -Force -Recurse -Path "C:\MyFolder"

Produces this error, but

Remove-Item -Force -Recurse -Path "C:\MyFolder\*"

Does not.

So don't forget the magic sauce

Solution 4 - Powershell

this worked for me where i deleted files and folders older then i year recursively including folders.

Get-ChildItem -Directory -Path X:\AutomateCache | where-Object {$_.Lastwritetime -ile (get-date).AddMonths(-12) } | Remove-Item -Force -Recurse -Verbose

Solution 5 - Powershell

This way works every time, it sorts the files and directories in descending order to ensure the deepest members of the directory structure are deleted first.

Get-ChildItem $output_path -File -Recurse | Sort-Object FullName -Descending | Remove-Item -Force -Confirm:$false;
Get-ChildItem $output_path -Directory -Recurse | Sort-Object FullName -Descending | Remove-Item -Force -Confirm:$false;
Remove-Item $output_path -Force;

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
QuestionHelloWorldView Question on Stackoverflow
Solution 1 - PowershellRichardView Answer on Stackoverflow
Solution 2 - PowershellKellen StuartView Answer on Stackoverflow
Solution 3 - PowershellNick.McDermaidView Answer on Stackoverflow
Solution 4 - PowershellVibhu BhatnagarView Answer on Stackoverflow
Solution 5 - PowershellNick DanielsView Answer on Stackoverflow