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 type 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 "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 delete option, You want to delete libraries in bulk, etc. Here is the PowerShell to delete document library in SharePoint 2013.

Delete document library in SharePoint using PowerShell:

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

#delete sharepoint document library using powershell 
This removes 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, lets 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
        [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)
            #Set Allow Delete Flag
            $Library.AllowDeletion = $true
            #delete document library from sharepoint using powershell - Send library to Recycle bin
            $Library.Recycle() | Out-Null
            #TO permanently delete a library, Use: 
            Write-Host "Library: $($LibraryName) deleted successfully from: $($WebURL)" -f Green
            Write-Host "Library: $($LibraryName) doesn't exist at $($WebURL)" -f Red
    catch { 
        write-host "Error Deleting Library..." $_.Exception.Message
    finally {
        $ErrorActionPreference = "Continue"

#Call the function to delete library
Delete-SPDocLibrary "" "Documents"
Here is my another post on how to delete a document library in SharePoint Online: Delete Document Library in SharePoint Online using PowerShell

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.