How to Delete a SharePoint List using PowerShell?
Problem: A faulty solution installed on the SharePoint farm created duplicate lists in all sites in a large site collection with 100+ sites in it. We had to remove them all!
To delete a list from SharePoint, from the browser, we can go to: View All Site Content >> Click on “Remove” link from the context menu of the list. Alternatively, You can get into List Settings >> click on “Delete this list” to remove a list in SharePoint.
However, There are 100+ sites where a particular list needs to be deleted.
Delete SharePoint list using PowerShell
PowerShell command to delete list in SharePoint:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Site collection URL
$SiteUrl="https://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 "https://Your-Site-URL" | Where-Object { $_.Lists.Delete([System.Guid]$_.Lists["Your-List-Name"].ID) }
SharePoint PowerShell to Delete List by GUID
Get-SPWeb "https://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 "https://intranet.crescent.com" "ConfigListv1"
So, call the function by looping through sites:
#Site URL variable
$SiteURL ="https://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 corrupt SharePoint list using PowerShell:
In some cases, you need PowerShell’s help when you are unable to delete the list from SharePoint web UI as the list is corrupted and SharePoint won’t let you remove it, Use this script to remove the SharePoint list using PowerShell: Force delete corrupted list in SharePoint using PowerShell
I have a workflow history list that is 10 GB with 30 million items in it. Do you think the script below is the best script to delete it? I ran the script 20 minutes ago and it’s still running.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Site collection URL
$SiteUrl=”https://mycompany.com”
$ListName = “Workflow History”
#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!”
Do not delete the workflow history list. That may break your workflow. Instead, purge the items in the workflow history list (if you don’t need to preserve it.) : How to delete all items from a SharePoint list using PowerShell?
I did create a new workflow history list for all the workflows. With that said, do you still recommend against deleting the Workflow History list?
Well and good then!
Thank you very much
yes it helped me !