Saturday, February 1, 2014

How to Delete a SharePoint List or Library using PowerShell

Problem: A faulty solution installed and deployed created duplicate lists in all sites in a large site collection with 100+ sites in it. Had to remove them all!

Solution: Delete SharePoint list using PowerShell
PowerShell command to delete list in SharePoint:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Site collection URL 
$SiteUrl="http://your-sharepoint-site" 
$ListName = "List-Name-To-Delete"

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

#Reset the "Allow Deletion" Flag
$list.AllowDeletion = $true
$list.Update()

$list.Delete()

write-host "List has been deleted successfully!"

One liner to delete a SharePoint list using PowerShell:
Get-SPWeb "http://Your-Site-URL" | Where-Object { $_.Lists.Delete([System.Guid]$_.Lists["Your-List-Name"].ID) }

SharePoint 2010 PowerShell delete list by GUID
Get-SPWeb "http://your-SharePoint-Site-URL" | Where-Object { $_.Lists.Delete([System.Guid]"{43c30f3a-f39a-4410-976c-a87bebb36e42}") }
Just replace the Web URL and GUID with yours and run the script.

Lets make a custom function to delete SharePoint list using PowerShell script.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Custom function to delete SharePoint list or document library using powershell
Function Delete-SPList
{
    param
    (
        [string]$WebURL  = $(throw "Please Enter the Web URL!"),
        [string]$ListName = $(throw "Please Enter the List Name to Delete!")
    )
    
    #Get the Objects 
    $Web = Get-SPWeb $WebURL
    $List = $Web.lists[$ListName]
 
    if($List)
    {
        #Set Allow Delete Flag
        $list.AllowDeletion = $true
        $list.Update()

        #delete list from sharepoint using powershell - Send List to Recycle bin
        $list.Recycle()
        
        #TO permanently delete a list, Use: 
        #$List.Delete()

        Write-Host "List: $($ListName) deleted successfully from: $($WebURL)"
    }
    else
    {
        Write-Host "List: $($ListName) doesn't exist at $($WebURL)"
    }
    $web.Dispose() 
}

#Call the function to delete list
Delete-SPList "http://intranet.crescent.com" "ConfigListv1"

So, call the function by looping through sites:
#Site URL variable
$SiteURL ="http://intranet.crescent.com" 

#get the site collection
$Site = Get-SPSite $SiteURL

#Loop through each site in the site collection
Foreach($web in $site.Allwebs)
{
    #Call the function to delete list
    Delete-SPList $web.URL "ConfigListv1"
}

Force delete delete corrupt SharePoint list using PowerShell:
In some cases, you need PowerShell's help when you are unable to delete list from SharePoint web UI as he list is corrupted and SharePoint won't let you to remove it. Use the above codes to remove SharePoint list using powershell.

More info here: Force delete corrupted list in SharePoint using PowerShell



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...