kwizcom banner advertisement

SharePoint Online: How to Delete a Document Library using PowerShell?

Requirement: Delete a Document Library in SharePoint Online using PowerShell

How to Delete a Document Library in SharePoint Online?
To delete a document library in SharePoint Online, follow these steps:
  • Login to your SharePoint Online site >> Click on Settings gear >> Select Site contents from the menu.
  • On the site contents page, Hover over the document library you want to delete and then click the context menu. 
  • From the menu, click on "Remove" and then confirm the prompt to send the document library to the Recycle Bin. 
    sharepoint online powershell delete document library
  • Alternatively, you can remove a document library in SharePoint Online by going to library settings >> Click on "Delete this Document Library" under Permissions and Management group.
    delete document library sharepoint online powershell
Quite simple, huh? Well, what if you don't find the delete option from the context menu or even in the document library's settings? What if you want to bulk delete multiple document libraries? PowerShell can help! Here is the SharePoint Online PowerShell to delete document library.

PowerShell to Delete a Document Library in SharePoint Online
Lets delete a document library in SharePoint Online using PowerShell
#Custom function to delete a SharePoint Online document library using powershell
Function Delete-SPODocumentLibrary
{
    param
    (
        [string]$SiteURL  = $(throw "Please Enter the Site URL!"),
        [string]$LibraryName = $(throw "Please Enter the Library Name to Delete!")
    )
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Document Library and set allow deletion flag
        $Library=$Ctx.Web.Lists.GetByTitle($LibraryName) 
        $Library.AllowDeletion = $True
        $library.Update()
        $Ctx.ExecuteQuery()
            
        #Delete the Document Library - Send to Recycle bin
        $Library.Recycle() | Out-Null  #To permanently delete, call: $Library.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host "Document Library: '$LibraryName' has been Deleted Successfully!" -ForegroundColor Green  
    }
    Catch {
        write-host -f Red "Error Deleting Document Library!" $_.Exception.Message
    }
}
 
#Call the function to delete a document library
Delete-SPODocumentLibrary -SiteURL "https://crescenttech.sharepoint.com/sites/Marketing/" -LibraryName "Team Documents"
SharePoint Online: How to Delete a Document Library using PowerShell? SharePoint Online: How to Delete a Document Library using PowerShell? Reviewed by Salaudeen Rajack on January 26, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.