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?

Document libraries are a vital part of SharePoint Online and are ideal for storing files you need to access frequently. However, there may be times you want to delete a document library that you no longer need. This can be done relatively quickly, but it’s important to remember that all the files and folders stored in the library will also be deleted. So, be sure to back up any important documents first!

To delete a document library in SharePoint Online, follow these steps:

  1. Login to SharePoint Online, Navigate to the document library
  2. Click on Settings >> Library Settings
    sharepoint online powershell delete document library
  3. Under Permissions and Management, click on the “Delete this document library” link.
    How to delete document library in SharePoint Online
  4. 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 the “Site Contents” page as well. Hover over the document library, and click on “Remove” from the context menu to delete the document library.

sharepoint online delete document library

Deleting a document library is a pretty straightforward process and can be accomplished in just a few clicks. Alright, let’s delete a 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 the document library? Are you getting the “This list cannot be deleted.” error message when trying to delete a document library, or not getting the “Delete this document library” link in document library settings? Well, to delete this sort of document library, you should set: the “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

Use the Remove-PnPList cmdlet to remove a document library in SharePoint Online. 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 -Interactive 
         
#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!

Delete All Empty Document Libraries in SharePoint Online using PowerShell

This time, we’ve got a requirement to remove all empty document libraries from a site! If you’re like me, you’ve probably created too many document libraries in SharePoint Online. And if you’re like me, you also probably want to clean them up from time to time. PowerShell to the rescue!

#Parameter
$SiteURL = "https://Crescent.sharepoint.com/"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
$ExcludedLibraries = @("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates", "Site Collection Documents","Site Collection Images",
                        "Reporting Templates","Solution Gallery", "Style Library", "Web Part Gallery","Site Assets", "wfpub", "Site Pages", "Images")

#Get All Empty Document Libraries to Delete
$EmptyLibraries = Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $ExcludedLibraries -notcontains $_.Title -and  $_.ItemCount -eq 0}

#Delete each empty document library
ForEach($Library in $EmptyLibraries)
{
    #Delete document library in sharepoint online using powershell
    #Remove-PnPList -Identity $Library -Recycle -Force
    Write-host -f Green "Library '$($Library.Title)' Deleted Successfully!"
}

I’ve commented on Line#17 intentionally. Just run the script once and make sure no other important document libraries are about to be deleted, and uncomment the line (by removing # from it!) to actually delete the empty document library. If you want to exclude any empty library from deletion, just add its name to the $ExcludedLibraries array.

How do I delete a SharePoint Online list?

To delete a SharePoint Online list, navigate to the list >> Click on “Settings” gear >> List settings >> Select “Delete this list” link. Confirm the deletion by clicking “OK”. If you do not see the “Delete this list” option, you may not have permission to delete the list. You can also delete a list from its context menu on the “Site contents” page.
More Info: How to delete a list in SharePoint Online?

How do I delete all files from a SharePoint Online Library?

Navigate to your SharePoint Online document library in the browser >> Click the check mark to select all files >> Click on the “Delete” button on the top link bar to remove all the files. Click the “Delete” button on the Delete dialog box to confirm the deletion.
More info: PowerShell to delete all files in SharePoint Online Document Library

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

6 thoughts on “SharePoint Online: PowerShell to Delete a Document Library

  • I have a document library of 600 000 items. It cant be deleted this way there are some limitations. (it cannot be deleted via GUI either) I am also trying this: Get-PnPListItem -List $LibraryName -Fields “ID” -PageSize 100 -ScriptBlock { Param($items) $items | Sort-Object -Descending | ForEach-Object{ $_.DeleteObject() } }

    It worked for a some thousands, but now I am hitting some sort of treshold again.

    Any ideas?

    Reply
  • Hi, is there a way instead of adding document libraries to exclude that we can exclude any libraries with files in it? 4000 libraries with around 50% having documentation in them make this a rather laborious task. Unless there was a way to run a script for a full library breakdown including child count?

    Reply
  • Great script, it works!

    Reply

Leave a Reply

Your email address will not be published.