OriBeta
OriBeta

Reputation: 11

How to use PowerShell to remove nonempty folder in OneDrive?

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

Upvotes: 1

Views: 2173

Answers (4)

Rohit Singh
Rohit Singh

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

skywalkerSam
skywalkerSam

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

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

Upvotes: 0

Nic Tanghe
Nic Tanghe

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

Dieter Roelants
Dieter Roelants

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

Related Questions