kwizcom banner advertisement

SharePoint Online: PowerShell to Delete a Document Library

How to delete document library in SharePoint Online?
To delete document library in SharePoint Online, follow these steps:
  • Login to SharePoint Online, Navigate to the document library
  • Click on Settings >> Library Settings
    sharepoint online powershell delete document library
  • Under Permissions and Management, click on "Delete this document library" link.
    How to delete document library in SharePoint Online
  • Confirm the prompt "This document library will be removed and all its files will be deleted.  Are you sure you want to send this document library to the site Recycle Bin?" with "Yes" once.
This sends the document library to the recycle bin. You can also remove a document library in SharePoint Online from "Site Contents" page. Simply hover over the document library, Click on "Remove" from the context menu to delete the document library.
sharepoint online delete document library

SharePoint Online: PowerShell to Delete a Document Library
Lets use PowerShell to delete a document library in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com/"
$DocLibName="Docs"

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $credentials
   
    #Get the Document Library
    $DocLibrary = $Ctx.web.Lists.GetByTitle($DocLibName)
    $Ctx.Load($DocLibrary)
    $Ctx.ExecuteQuery()

    If($DocLibrary -ne $Null)
    {
        #Delete the Document Library - Send to Recycle bin
        $DocLibrary.Recycle()

        #To Delete the Document Library - Permanently
        #$DocLibrary.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host -f Green "Document Library Deleted Successfully!"
    }
}
Catch {
    write-host -f Red "Error Deleting Document Library!" $_.Exception.Message
}
Please note, the "Recycle()" method deletes and sends the document library to the recycle bin and the "DeleteObject()" method permanently deletes the document library from SharePoint Online.


Unable to delete document library in SharePoint Online?
Getting "This list cannot be deleted." error message when trying to delete a document library or not getting "Delete this document library" link in document library settings? Well, to delete this sort of document libraries, you should set: AllowDeletion property to $True first and then delete the document library.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com/"
$DocLibName="Project Documents"

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $credentials
   
    #Get the Document Library
    $DocLibrary = $Ctx.web.Lists.GetByTitle($DocLibName)
    $Ctx.Load($DocLibrary)
    $Ctx.ExecuteQuery()

    #Set Allow Deletion Property to True
    $DocLibrary.AllowDeletion = $True
    $DocLibrary.Update()

    If($DocLibrary -ne $Null)
    {
        #Delete the Document Library - Send to Recycle bin
        $DocLibrary.Recycle()

        #To Delete the Document Library - Permanently
        #$DocLibrary.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host -f Green "Document Library Deleted Successfully!"
    }
}
Catch {
    write-host -f Red "Error Deleting Document Library!" $_.Exception.Message
}
SharePoint Online: PowerShell to Delete a Document Library SharePoint Online: PowerShell to Delete a Document Library Reviewed by Salaudeen Rajack on June 19, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.