Can't delete user OneDrive subfolder

Aase Nomad 246 Reputation points
2022-02-21T17:34:23.783+00:00

We are planning to delete a user OneDrive folders using PowerShell but not sure why we are unable to do it for some subfolders.
The user Hold has been removed 2 weeks ago and he doesn't have any retention policy applied to him either.

In his Parent Folder, There were 5 Subfolders. I was able to delete the other 3 subfolders already but I wasn't able to do it for the remaining 2.

176492-image.png

Anybody know why it might be?
176532-image.png

The other 3 Subfolders that I already deleted also have a few nested subfolder too but I didn't get this errors for them.

   [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12  
   [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12;  
     
   Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"   
   Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"   
   Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"   
     
     
     
   #Config Variables  
   $SiteURL = "https://companyName-my.sharepoint.com/personal/userid/"  
   $ListName = "Documents"  
   $FolderServerRelativeURL = "/personal/user_id/Documents/PC-OFFBOARD"  
     
   Try {  
       #Connect to PnP Online  
       Connect-PnPOnline -Url $SiteURL -interactive  
       $List = Get-PnPList $ListName  
     
             
       #Get All Items from Folder in Batch  
       $global:counter = 0;  
       $ListItems = Get-PnPListItem -List $ListName -PageSize 5000 -Fields ID, FileDirRef, FileRef -ScriptBlock `  
       { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `  
               "Getting Items from Folder '$FolderServerRelativeURL'" -Status "Getting Items $global:Counter of $($List.ItemCount)"; }  
      
       #Get List Items from the folder and Sort List Items based on Server Relative URL - Descending  
       $ItemsFromFolder = $ListItems | Where { $_.FieldValues["FileDirRef"] -match $FolderServerRelativeURL } | Select-Object @{Name = "ServerRelativeURL"; Expression = { $_.FieldValues.FileRef } }, ID | Sort-Object -Descending -Property ServerRelativeURL  
       Write-host "Total Number of Items Found in the Folder:"$ItemsFromFolder.count  
               
       #Delete List Items in Batch  
       $Batch = New-PnPBatch  
       ForEach ($Item in $ItemsFromFolder) {  
           Remove-PnPListItem -List $ListName -Identity $Item.ID -Recycle -Batch $Batch  
           Write-host "Item Queued for deletion:"$Item.ServerRelativeURL  
           Invoke-PnPBatch -Batch $Batch -Details  
       }  
       Invoke-PnPBatch -Batch $Batch -Details  
      
   }  
   Catch {  
       write-host "Error: $($_.Exception.Message)" -foregroundcolor Red  
   }  
Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office OneDrive For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. CaseyYang-MSFT 10,461 Reputation points
    2022-02-22T06:47:29.537+00:00

    Hi @Aase Nomad ,

    According to your PowerShell error message, you are not able to delete some specific files in the folder. About Exception calling "ExecuteQuery" with "0" argument(s)" "File Not Found." error, you could try to change the name of the error file then delete it again.

    Did you run SharePoint Online Management Shell as a administrator? Some files may need administrator permission to delete.

    You could use following PnP PowerShell to bulk delete files in SharePoint Online.

    #Parameters  
    $SiteURL = "https://xxx.sharepoint.com/sites/xxx"  
    $ListName = "xxx"  
       
    #Connect to the Site  
    Connect-PnPOnline -URL $SiteURL -Interactive  
       
    #Get the web & document Library  
    $Web = Get-PnPWeb  
    $List = Get-PnPList -Identity $ListName  
       
    #Function to delete all items in a folder - and sub-folders recursively  
    Function Delete-AllFilesFromFolder($Folder)  
    {  
        #Get the site relative path of the Folder  
        If($Folder.Context.web.ServerRelativeURL -eq "/")  
        {  
            $FolderSiteRelativeURL = $Folder.ServerRelativeUrl  
        }  
        Else  
        {         
            $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.web.ServerRelativeURL,[string]::Empty)  
        }  
       
        #Get All files in the folder  
        $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File  
           
        #Delete all files  
        ForEach ($File in $Files)  
        {  
            Write-Host ("Deleting File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)  
               
            #Delete Item  
            Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle  
        }  
       
        #Process all Sub-Folders  
        $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder  
        Foreach($Folder in $SubFolders)  
        {  
            #Exclude "Forms" and Hidden folders  
            If( ($Folder.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_"))))  
            {  
                #Call the function recursively  
                Delete-AllFilesFromFolder -Folder $Folder  
            }  
        }  
    }  
       
    #Get the Root Folder of the Document Library and call the function  
    Delete-AllFilesFromFolder -Folder $List.RootFolder  
    

    For Reference: SharePoint Online: Delete All Files in a Document Library using PowerShell


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.