# Remove-Item : Access to the cloud file is denied while deleting files from OneDrive

I like OneDrive. It allows me to keep my data secure and always synchronized. If things go wrong, I can always get it back. I use it for almost everything. Even for my PowerShell projects, which are committed to GitHub, so in theory, I shouldn’t need that. But every once in a while, I make some stupid mistake and delete a file that has yet not been committed to GitHub, and that’s where the OneDrive comes in handy. Quick restore, and we’re back. Unfortunately, sometimes things aren’t as I would expect them to work. For example, let’s have a look at this nice list of markdown files that are documentation for my module called GPOZaurr.

blog

06 Dec 2020 pl

![Remove-Item : Access to the cloud file is denied while deleting files from OneDrive](/wp-content/uploads/2020/01/PowerShellBlack.png)

[Zobacz obrazy (3)](#pf-content-image-1-0)

I like **OneDrive**. It allows me to keep my data secure and always synchronized. If things go wrong, I can always get it back. I use it for almost everything. Even for my **PowerShell** projects, which are committed to **GitHub**, so in theory, I shouldn't need that. But every once in a while, I make some stupid mistake and delete a file that has yet not been committed to **GitHub**, and that's where the **OneDrive** comes in handy. **Quick restore, and we're back**. Unfortunately, sometimes things aren't as I would expect them to work. For example, let's have a look at this nice list of markdown files that are documentation for my module called [GPOZaurr](https://github.com/EvotecIT/GPOZaurr).

[!\[Pliki dokumentacji Markdown w folderze GPOZaurr Docs przechowywanym w OneDrive.\](/wp-content/uploads/2020/12/img_5fccf47036349.png)](/wp-content/uploads/2020/12/img_5fccf47036349.png)

Every time I would like to delete that content or anything from PowerShell it would throw me an error

> Remove-Item : Access to the cloud file is denied

[!\[Blad PowerShell Remove-Item informujacy, ze dostep do pliku w chmurze zostal zabroniony.\](/wp-content/uploads/2020/12/img_5fccf4b608eba.png)](/wp-content/uploads/2020/12/img_5fccf4b608eba.png)

While I could understand the error if it was a file that is stored in the cloud, it's actually fully available on my drive, just synchronized to **OneDrive**. It wasn't always like that. It started happening a few months ago, so most likely some update caused it – or maybe it's the great **Files On-Demand** feature causing this issue. I don't know and I don't even want to go deep and test every possible combination where this happens. All I know is this impacts my scripts, as anything that is supposed to get deleted automatically and, for some reason, is stored on **OneDrive** gets hit with this error.

### 💡 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.

```powershell
Remove-Item -LiteralPath "C:\Support\GitHub\GpoZaurr\Docs" -Force -Recurse
```

[!\[PowerShell probujacy rekurencyjnie usunac folder Docs synchronizowany przez OneDrive.\](/wp-content/uploads/2020/12/img_5fccfa025e345.png)](/wp-content/uploads/2020/12/img_5fccfa025e345.png)

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.

```powershell
$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**.

```powershell
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

```powershell
$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:

```powershell
$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?

```powershell
$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

```powershell
function Remove-ItemAlternative {

    [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 {

    [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.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:B3167E702E0014654ADF58C883EF03BE58F6F562D5B62DEB30E1E65EBE850CE5 -->
