1

In Powershell I have a script which should clear out a folder of project files retrieved from TFS.

The command is simply:

$deletePath = 'c:\Compile\*'
Remove-Item $deletePath -Recurse -Force

Before running there are about 150 folders in c:\Compile, afterwards there are 3 folders left.

The script generates 5 errors

Remove-Item : Cannot remove item C:\Compile\DatabaseObjects: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\IFIFWCHG\obj: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\IFIFWCHG: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\PrecompiledWeb\IIS: The directory is not empty.
Remove-Item : Cannot remove item C:\Compile\PrecompiledWeb: The directory is not empty.

I thought because of the use of -Force it handled the files/folders in directories.

The funny thing is, the DatabaseObjects folder is empty after the script runs, so it seems like it is trying to delete the folder before deleting the files.

andrewb
  • 2,995
  • 7
  • 54
  • 95
  • Does this answer your question? [Cannot remove item. The directory is not empty](https://stackoverflow.com/questions/38141528/cannot-remove-item-the-directory-is-not-empty) – Martin Backasch Oct 29 '20 at 08:03

1 Answers1

2

Try to retrieve all items using the Get-ChildItem cmdlet and use a ForEach-Object loop to iterate over them and delete.

Get-Childitem 'c:\Compile\' -Recurse | ForEach-Object { 
    Remove-Item $_.FullName -Force
}
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • This can fail when deleting nuget `packages` if you have any locks on the file via VS IDE, be sure to close any applications that can prevent this command from succeeding! – SliverNinja - MSFT May 15 '18 at 23:24