Sharepoint PS to delete specific folder and contents

Ernesto Martínez 5 Reputation points
2023-01-16T10:13:39.1366667+00:00

Hi,

One of our user requests from us to delete a folder and all of it's content in the "Documents" library of a Sharepoint site. This folder contains like 60k sub-folders...

We have a Retention policy applied to all Sharepoint/Teams/OneDrive and this particular site and it's group have already been excluded from them.

The folder is located in a path "sites/Documents/Folder1/Folder2/Folder3"

I've looking for a script that deletes the content in Folder3, including sub-folders and documents, but all of them that I tried didn't work.

Any help is much appreciated.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,163 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Renjie Sun-MSFT 2,861 Reputation points Microsoft Employee
    2023-01-17T02:50:57.72+00:00

    Hi @Ernesto Martínez

    Thanks for the post.

    You could refer to the following PowerShell command template to delete folder with files in SharePoint Online:

    #Config Variables
    $SiteURL = "your-site-url"
    $ListName ="listname"
    $FolderServerRelativeURL = "/sites/sitename/listname/foldername"
     
    Try {
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteURL -Interactive
          
        #Get All Items from Folder in Batch
        $ListItems = Get-PnPListItem -List $ListName -FolderServerRelativeUrl $FolderServerRelativeURL -PageSize 2000 | Sort-Object ID -Descending
      
        #Powershell to delete all files from a folder
        ForEach ($Item in $ListItems)
        {
            Remove-PnPListItem -List $ListName -Identity $Item.Id -Recycle -Force
            Write-host "Removed File:"$Item.FieldValues.FileRef
        }
    }
    Catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
    
    

    For more information about Delete All Files and Sub-Folders from a Folder Recursively using PowerShell in SharePoint Online

    I hope I can offer you a better experience next time if I have a chance to work with you again.

    Yours faithfully,

    Renjie Sun


    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.

    0 comments No comments

  2. Ernesto Martínez 5 Reputation points
    2023-01-17T08:09:34.29+00:00

    Hello Renjie Sun,

    Thank you for the quick response!

    It looks good but I'm getting an error refering to the "The attempted operation is prohibited because it exceeds the list view threshold" with the Get-PnPListItem.Captura1

    I have tried changing the "-PageSize" to 5000 but it still keeps repeating.

    Thank you again for your support.


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.