SharePoint Online: PowerShell to Delete a Document Library

Requirement: Delete SharePoint Online Document Library using PowerShell

How to delete a document library in SharePoint Online?
To delete a 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 as well. Simply hover over the document library, click on "Remove" from the context menu to delete the document library.
sharepoint online delete document library
Alright, let's delete SharePoint Online document library using PowerShell!

SharePoint Online: PowerShell to Delete a Document Library
Here is the 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 a document library in SharePoint Online?
Cannot delete document library? Are you 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
}

PnP PowerShell to Delete SharePoint Online Document Library
Here is the PnP PowerShell to delete a document library in SharePoint Online.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName ="Work"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin 
         
#Check if document Library exist
If(Get-PnPList -Identity $LibraryName)
{
    #powershell to remove document library
    Remove-PnPList -Identity $LibraryName -Force -recycle
    Write-host -f Green "Document Library '$LibraryName' Deleted Successfully!"
}
Else
{
    Write-host -f Yellow "Could not find Library '$LibraryName'"
}
This script deletes the document library and sends it to the recycle bin!

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.