💡 Remove-Item – Workaround for Access to the cloud file is denied issue
It seems the error only happens when trying to delete folders that contain those files.
Remove-Item -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs" -Force -Recurse
It seems the problem comes from a directory, not the files themselves, so using a small workaround using Get-ChildItem to list and then delete all files helps to get rid of all files.
$Items = Get-ChildItem -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs" -Recurse
foreach ($Item in $Items) {
Remove-Item -LiteralPath $Item.Fullname
}
However retrying again deletion of a folder, even without files, still gives an error when using Remove-Item.
Remove-Item -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs" -Force -Recurse
Remove-Item : Access to the cloud file is denied
At C:\Support\GitHub\GpoZaurr\Examples\Test.ps1:10 char:1
Did you know Remove-Item is not the only way to delete files and folders in PowerShell? Using Get-Item or Get-ChildItem, you're able to use Delete() method
$Item = Get-Item -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs"
$Item.Delete($true)
To my surprise, the following method worked without a problem. This means to fully delete the folder with all files in it, we can use:
$Items = Get-ChildItem -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs" -Recurse
foreach ($Item in $Items) {
Remove-Item -LiteralPath $Item.Fullname
}
$Items = Get-Item -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs"
$Items.Delete($true)
Since we're already using the Delete() method for a folder, why not try to do the same for the whole thing?
$Items = Get-ChildItem -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs" -Recurse
foreach ($Item in $Items) {
$Item.Delete()
}
$Items = Get-Item -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs"
$Items.Delete($true)
Finally, I wanted to have a quick and easy way to reuse my code
function Remove-ItemAlternative {
<#
.SYNOPSIS
Removes all files and folders within given path
.DESCRIPTION
Removes all files and folders within given path. Workaround for Access to the cloud file is denied issue
.PARAMETER Path
Path to file/folder
.PARAMETER SkipFolder
Do not delete top level folder
.EXAMPLE
Remove-ItemAlternative -Path "C:\Support\GitHub\GpoZaurr\Docs"
.EXAMPLE
Remove-ItemAlternative -Path "C:\Support\GitHub\GpoZaurr\Docs"
.NOTES
General notes
#>
[cmdletbinding()]
param(
[alias('LiteralPath')][string] $Path,
[switch] $SkipFolder
)
if ($Path -and (Test-Path -LiteralPath $Path)) {
$Items = Get-ChildItem -LiteralPath $Path -Recurse
foreach ($Item in $Items) {
if ($Item.PSIsContainer -eq $false) {
try {
$Item.Delete()
} catch {
Write-Warning "Remove-ItemAlternative - Couldn't delete $($Item.FullName), error: $($_.Exception.Message)"
}
}
}
$Items = Get-ChildItem -LiteralPath $Path -Recurse
foreach ($Item in $Items) {
try {
$Item.Delete()
} catch {
Write-Warning "Remove-ItemAlternative - Couldn't delete $($Item.FullName), error: $($_.Exception.Message)"
}
}
if (-not $SkipFolder) {
$Item = Get-Item -LiteralPath $Path
try {
$Item.Delete($true)
} catch {
Write-Warning "Remove-ItemAlternative - Couldn't delete $($Item.FullName), error: $($_.Exception.Message)"
}
}
} else {
Write-Warning "Remove-ItemAlternative - Path $Path doesn't exists. Skipping. "
}
}function Remove-ItemAlternative {
<#
.SYNOPSIS
Removes all files and folders within given path
.DESCRIPTION
Removes all files and folders within given path. Workaround for Access to the cloud file is denied issue
.PARAMETER Path
Path to file/folder
.PARAMETER SkipFolder
Do not delete top level folder
.EXAMPLE
Remove-ItemAlternative -Path "C:\Support\GitHub\GpoZaurr\Docs"
.EXAMPLE
Remove-ItemAlternative -Path "C:\Support\GitHub\GpoZaurr\Docs"
.NOTES
General notes
#>
[cmdletbinding()]
param(
[alias('LiteralPath')][string] $Path,
[switch] $SkipFolder
)
if ($Path -and (Test-Path -LiteralPath $Path)) {
$Items = Get-ChildItem -LiteralPath $Path -Recurse
foreach ($Item in $Items) {
if ($Item.PSIsContainer -eq $false) {
try {
$Item.Delete()
} catch {
Write-Warning "Remove-ItemAlternative - Couldn't delete $($Item.FullName), error: $($_.Exception.Message)"
}
}
}
$Items = Get-ChildItem -LiteralPath $Path -Recurse
foreach ($Item in $Items) {
try {
$Item.Delete()
} catch {
Write-Warning "Remove-ItemAlternative - Couldn't delete $($Item.FullName), error: $($_.Exception.Message)"
}
}
if (-not $SkipFolder) {
$Item = Get-Item -LiteralPath $Path
try {
$Item.Delete($true)
} catch {
Write-Warning "Remove-ItemAlternative - Couldn't delete $($Item.FullName), error: $($_.Exception.Message)"
}
}
} else {
Write-Warning "Remove-ItemAlternative - Path $Path doesn't exists. Skipping. "
}
}
It seems I have no other choice than use this alternative approach to delete files and folders from my OneDrive, at least until Microsoft fixes this issue. Hopefully, sooner rather than later. Keep in mind that this function above is fundamental, with minimal error handling. If you intend to use it, make sure to add some error handling yourself. It's supposed to be a workaround rather than a permanent solution.