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. If you need to delete a document library that’s no longer needed in SharePoint, we will provide step-by-step instructions on deleting a document library in SharePoint. We’ll also show you how to use PowerShell to delete a document library and all its contents.

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:

If you need to delete a document library in SharePoint, PowerShell can make the process a lot easier. Let me show you how to use PowerShell to delete a document library:

$WebURL="https://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

Deleting a document library also deletes all of its contents (Obviously!). So, make sure you have backups in place and proceed with deleting the document library:

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 "https://intranet.crescent.com/" "Documents"

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

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!

Leave a Reply

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