How to Delete a Document Library in SharePoint using PowerShell?

Task: Delete document library in SharePoint with PowerShell

How to Delete a Document Library in SharePoint?

Document Libraries are used to store and manage Microsoft Office documents, PDF files, and other types of files within SharePoint. To delete a document library in SharePoint, follow these steps:

  • Browse to your SharePoint Site
  • Click on Site Settings gear >> Site Contents
  • Click on the “Remove” link from the context menu of the library. Alternatively, You can go to Library Settings >> Click on “Delete this document library” to remove a document library in SharePoint.powershell delete document library sharepoint 2013

Now, there are scenarios where you want to utilize PowerShell to remove a document library in SharePoint, such as SharePoint UI doesn’t provide a delete option, You want to delete libraries in bulk, etc. Here is the PowerShell to delete a document library in SharePoint 2013.

Delete document library in SharePoint using PowerShell:

$WebURL="http://intranet.crescent.com/"
$LibraryName="Documents"

#Get Web and List objects
$web = Get-SPWeb $WebURL
$list = $web.Lists[$LibraryName]

#delete sharepoint document library using powershell 
$list.Delete()

This removes the document library in SharePoint 2010 with PowerShell. While the above script is fairly simple and permanently deletes the given document library from SharePoint – without sending it to recycle bin, let us add some error handling and make it a re-usable function.

Delete SharePoint document library using PowerShell

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Custom function to delete SharePoint document library using powershell
Function Delete-SPDocLibrary
{
    param
    (
        [string]$WebURL  = $(throw "Please Enter the Web URL!"),
        [string]$LibraryName = $(throw "Please Enter the Library Name to delete!")
    )
    try {
        $ErrorActionPreference = "Stop"   

        #Get the Objects 
        $Web = Get-SPWeb $WebURL
        $Library = (Get-SPWeb $WebURL).Lists.TryGetList($LibraryName)
  
        if($Library)
        {
            #Set Allow Delete Flag
            $Library.AllowDeletion = $true
            $Library.Update()
 
            #delete document library from sharepoint using powershell - Send library to Recycle bin
            $Library.Recycle() | Out-Null
         
            #TO permanently delete a library, Use: 
            #$Library.Delete()
 
            Write-Host "Library: $($LibraryName) deleted successfully from: $($WebURL)" -f Green
        }
        else
        {
            Write-Host "Library: $($LibraryName) doesn't exist at $($WebURL)" -f Red
        }
    }
    catch { 
        write-host "Error Deleting Library..." $_.Exception.Message
    }
    finally {
        $ErrorActionPreference = "Continue"
        $web.Dispose() 
    }
}

#Call the function to delete library
Delete-SPDocLibrary "http://intranet.crescent.com/" "Documents"

Here is my another post on how to delete a document library in SharePoint Online: Delete Document Library in SharePoint Online using PowerShell

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

Leave a Reply