Cannot remove item. The directory is not empty

后端 未结 3 942
心在旅途
心在旅途 2021-02-01 13:13

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

Remove-Item -Force -Recurse -Path $directoryPath

I am getting the error Cannot remo

相关标签:
3条回答
  • 2021-02-01 13:45

    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.

    0 讨论(0)
  • 2021-02-01 13:52

    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   
    
    0 讨论(0)
  • 2021-02-01 14:10

    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

    0 讨论(0)
提交回复
热议问题