Thursday, August 13, 2015

How to Delete a Document Library in SharePoint using PowerShell

Task: Delete document library in SharePoint with PowerShell

Solution: 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:
$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 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
{
    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"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...