1

Descriptions

Cannot remove nonempty folder in OneDrive directory

Step to reproduce

  1. Launch PowerShell in OneDrive directory
PS C:\Users\MyUserName\OneDrive>
  1. Try to use Remove-Item cmdlet to remove a nonempty folder in this directory, for example: the .\test\ folder
PS C:\Users\MyUserName\OneDrive> Remove-Item .\test\

Expected result

Without the -Recurse parameter, PowerShell should return a confirm message, such as

Confirm
The item at C:\Users\MyUserName\OneDrive\test\ has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

Actual result

PowerShell return a error message

Remove-Item: Cannot remove item C:\Users\MyUserName\OneDrive\test\: The directory is not empty. : 'C:\Users\MyUserName\OneDrive\test\'

Note

  1. PowerShell and Administrator:PowerShell get the same result;
  2. If I exit OneDrive process and create a new nonempty folder under OneDrive directory, PowerShell can remove it as normal (see Note 4., because unsynced folders do not have the ReparsePoint attribute);
  3. CMD can remove the folder successfully, which means I can use the below command in PowerShell to remove the folder too. But I want to accomplish my goal just by PowerShell cmdlet;
cmd.exe /C "rd /s test"
  1. Get-ChildItem cmdlet shows that the mode of normal folders (not synced by OneDrive) is 'd'(directory), but the mode of synced folders is 'l'(reparsepoint). Is this the reason that I cannot remove a folder under OneDrive directory as normal?

Version info

PSVersion:7.1.3

OS:Microsoft Windows 10.0.19042

OneDrive Version:21.052.0314.0001 (Office 365 A1)

Update

I try to remove the test folder on PowerShell 5 but fail, too.

The error message from PowerShell 5.1:

PS C:\Users\MyUserName\OneDrive> Remove-Item .\test\ -Force -Recurse
Remove-Item : Access to the cloud file is denied.
At line:1 char:1
+ rm .\test\ -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Remove-Item], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand
6
  • Pretty sure file and folder objects are treated differently by Remove-Item. If I recall a script I wrote to do something similar I needed to remove the files first before removing the folders. Commented Apr 10, 2021 at 11:41
  • What happens if you try with -Force ? Commented Apr 10, 2021 at 17:51
  • Try -Recurse. You should look at the docs. Commented Apr 11, 2021 at 11:24
  • @Vivere I tried and it did not work. And normally, even if without -Recurse , PowerShell should return a confirm message (just like the expected result in question) instead of an error message. Commented Apr 11, 2021 at 12:19
  • @SantiagoSquarzon I tried and it did not work. In fact, there is no hidden file or folder in the test folder, so I think -Force parameter is irrelevant. Commented Apr 11, 2021 at 12:21

4 Answers 4

1

I just hit the same thing and this worked for me:

Get-ChildItem -recurse .\test | Sort-Object -Property FullName -Descending | ForEach-Object { $_.Delete() }
(Get-Item test).Delete()

I'm rather new at PowerShell, so there might be more elegant or correct ways to do the above.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked but doesn't delete the folders, only files.
1

Previous answer did not handle hidden files, You can add these to your profile

function rmc ($file) {
    (Get-Item $file).Delete()
}


function rmd ($folder) {
    Get-ChildItem -recurse -force $folder | Sort-Object -Property FullName -Descending | ForEach-Object { $_.Delete() }
    (Get-Item $folder).Delete()
}

Comments

0

Onedrive folder's no different than any other folder in your system, the same folder removal PowerShell commands will be used. Here you go...

  1. cd into the Onedrive folder.
  2. Use, rm -r -fo <FolderName>

Comments

0

If someone came here to just delete the folders with non-empty sub folders, I used the OneDrive app on Windows 10 to delete the folders and when OneDrive synced, the folder got delete from the cloud as well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.