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
Let's delete a document library in SharePoint Online using PowerShell
#Custom function to delete a SharePoint Online document library using powershell
Function Delete-SPODocumentLibrary
        [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.AllowDeletion = $True
        #Delete the Document Library - Send to Recycle bin
        $Library.Recycle() | Out-Null  #To permanently delete, call: $Library.DeleteObject()

        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 "" -LibraryName "Team Documents"
Once deleted, they are moved in to the recycle bin and if necessary, we can restore them as long as they remain in the recycle bin (Technically, 93 days!)

Delete SharePoint Online Document Library using PnP PowerShell 
Lets delete document library in SharePoint Online with PowerShell PnP
#Config Variables
$SiteURL = ""

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Library to Delete
$Library = Get-PnPList -Identity $LibraryName -ErrorAction SilentlyContinue

#Check if Library exists
    #sharepoint online powershell to delete library
    Remove-PnPList -Identity $LibraryName -Recycle -Force
    Write-host -f Green "Library '$LibraryName' Deleted Successfully!"
    Write-host -f Yellow "Could not find Library '$LibraryName'"

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.