Reputation: 11
Cannot remove nonempty folder in OneDrive directory
PS C:\Users\MyUserName\OneDrive>
PS C:\Users\MyUserName\OneDrive> Remove-Item .\test\
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"):
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\'
cmd.exe /C "rd /s test"
PSVersion:7.1.3
OS:Microsoft Windows 10.0.19042
OneDrive Version:21.052.0314.0001 (Office 365 A1)
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
Upvotes: 1
Views: 2173
Reputation: 411
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
Upvotes: 0
Reputation: 1
Onedrive folder's no different than any other folder in your system, the same folder removal PowerShell commands will be used. Here you go...
cd
into the Onedrive folder.rm -r -fo <FolderName>
Upvotes: 0
Reputation: 39
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()
}
Upvotes: 1
Reputation: 11
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.
Upvotes: 1