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?

Deleting a document library is a quick and easy way to clean up your site and remove any unwanted content. Deleting a document library is as straightforward as deleting other types of lists in SharePoint Online. In this article, we’ll walk you through how to delete a document library in SharePoint Online step-by-step. We will also provide a PowerShell script that you can use to delete a document library!

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

  1. Login to your SharePoint Online site >> Click on Settings gear >> Select Site contents from the menu.
  2. On the site contents page, hover over the document library you want to delete and click the context menu. 
  3. 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
  4. Alternatively, you can remove a document library in SharePoint Online by visiting library settings >> Click “Delete this Document Library” under Permissions and Management group.
    delete document library sharepoint online powershell

Once you have completed the deletion process, the document library will be removed from the site, and its contents will be deleted (sent to the recycle bin!). To verify that the library has been deleted, return to the Site contents page and confirm that the library no longer appears in the list of available lists and libraries.

Quite simple, huh? What if you don’t find the delete option in the context menu or 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 the document library.

PowerShell to Delete a Document Library in SharePoint Online

While SharePoint Online provides a user-friendly interface for managing document libraries, deleting multiple libraries can be time-consuming and repetitive. With the help of PowerShell, we can automate this process to help save time and resources. As an organization grows, document libraries in SharePoint Online may become cluttered or outdated, negatively impacting productivity and making it difficult for users to find the information they need.

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
{
    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://Crescent.sharepoint.com/sites/Marketing/" -LibraryName "Team Documents"

Once deleted, they are moved into 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

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

  1. Connect to SharePoint Online using PowerShell by running the Connect-PnPOnline cmdlet.
  2. Use the Get-PnPList cmdlet to retrieve the document library you want to delete.
  3. Use the Remove-PnPList cmdlet, specifying the Name of the document library to delete.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$LibraryName="Branding"

#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
If($Library)
{
    #sharepoint online powershell to delete library
    Remove-PnPList -Identity $LibraryName -Recycle -Force
    Write-host -f Green "Library '$LibraryName' Deleted Successfully!"
}
Else
{
    Write-host -f Yellow "Could not find Library '$LibraryName'"
}

This PowerShell sends the document library to the recycle bin. If you are trying to delete a large document library in SharePoint Online, You may get an error, “Sorry, something went wrong: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.” The solution is: You have to delete all files first! How to Delete All Files in a SharePoint Online Document Library using PowerShell?

How to delete a folder from the SharePoint document library?

To delete a folder from the SharePoint document library, follow these steps:
Navigate to the SharePoint document library that contains the folder you want to delete >> Hover over the folder you want to delete and click the checkbox that appears to the left of the folder name >> Click the “Delete” button in the toolbar at the top of the page >> In the confirmation dialog box that appears, click “OK” to confirm the deletion.
Please note, deleting a folder will also delete all files and subfolders contained within it!

Can I use PowerShell to delete multiple document libraries at once?

Yes! You can use PowerShell to delete multiple document libraries at once by using a script or a loop. This can be more efficient than deleting each library manually.

Can I recover a deleted document library in SharePoint Online using PowerShell?

It is possible to recover a deleted document library in SharePoint Online within a certain timeframe (usually 93 days) using PowerShell. However, after this timeframe, the library may be permanently deleted and unrecoverable.

What is the easiest way to delete a document library in SharePoint Online?

The easiest way to delete a document library in SharePoint Online is to use the web interface. Simply navigate to the document library you want to delete, click on the “Settings” gear icon, select “Library settings,” and then click on the “Delete this document library” link at the bottom of the page. However, using PowerShell can be a more efficient option if you have many document libraries to delete.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “SharePoint Online: How to Delete a Document Library using PowerShell?

Leave a Reply

Your email address will not be published. Required fields are marked *