Delete list items
14/10/2012 22:17
$serverUrl="https://localhost/sites/sectionedlinks"
$listName="Sectioned Links Sections"
function DeleteListItems([string] $siteUrl, [string] $listName)
{
$spWeb = Get-SPWeb -Identity $serverUrl
$spList = $spWeb.Lists[$listName]
if ($spList -ne $null)
{
$count=$spList.Items.Count
if($count -ne 0)
{
write-host "Deleting items..."
foreach ($item in $spList.items)
{
#cannot delete $item directly, because enumerator fails
$deaditem=$splist.GetItemById($item.ID)
$deaditem.Delete()
}
write-host "List" $listName " items were deleted."
}
}
}
DeleteListItems -siteUrl $serverUrl -listName $listName
$SiteUrl="https://localhost/sites/sectionedlinks"
$listName="Sectioned Links Sections"
function DeleteList
{
param([string] $siteUrl,
[string] $listName)
try
{
$web=get-spweb $siteUrl
$list=$web.Lists[$listName]
if($list -ne $null)
{
write-host "Deleting '" $listName "' list..."
$list.Delete()
$web.Update()
write-host $listName " list was succesfuly deleted from " $siteUrl
}
else
{
write-host "List '" $listName "' doesn't exist on Url: " $siteUrl
}
}
catch [Exception]
{
write-error $_
}
finally
{
if($web -eq $null)
{
$web.Dispose()
}
}
}
#calling function
DeleteList -siteUrl $SiteUrl -listName $listName
#Delete lookup field
$web=get-spweb $siteUrl
$list=$web.Lists["Sectioned Links"]
$list.Fields
$list.Fields["SectionedLinksSection"]
$list.Fields["Section"]
$list.Fields["Section"].Delete()