-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete All List Items.ps1
86 lines (69 loc) · 4.71 KB
/
Delete All List Items.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#Script that deletes items in a list, limited by threshold defined in Sharepoint
# Configuration variables
$SiteURL = "http://XXXXXXXXXXXXX"
$ListName= "Last Added Items"
$Threshold= 50000
$RecycleFlag = $false
### DO NOT TOUCH THE CODE BELOW ###
# Required if not run in SharePoint Management Shell
if($ver.Version.Major -gt 1)
{
$Host.Runspace.ThreadOptions = "ReuseThread"
}
If((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.Sharepoint.Powershell
}
# Get web site
$web = Get-SPWeb $SiteURL;
If(!($web)) { Write-Output "Website not found. Verify URL" -ForegroundColor Red; Break;}
Else { Write-Output "URL is valid: $($web)"}
# Get specified list
$list = $web.Lists[$listName]
If(!($list)) { Write-Output "List not found. Verify URL" -ForegroundColor Red; Break;}
Else { Write-Output "List has $($list.itemCount) items"}
$itemsToDelete = $list.itemCount
# Delete all items according to the threshold
While ($list.ItemCount -ne 0)
{
Write-Output "Querying started - $(Get-Date -Format o)"
$query = New-Object Microsoft.Sharepoint.SPQuery;
$query.RowLimit = $threshold;
$caml = '<Where>
<Neq>
<FieldRef Name= "ID"/>
<Value Type = "Counter">0</Value>
</Neq>
</Where>'
$query.Query = $caml
$items = $list.GetItems($query)
Write-Output "Querying completed - $(Get-Date -Format o)"
If($items.Count -eq 0)
{
Write-Output "List is empty";
Exit 1;
}
Else
{
$StartTime = Get-Date -Format o
Write-Output "Deleting started at: $StartTime"
Write-Output $itemsLeft
Foreach($item in $items)
{
#Write-Output "Deleting item $($item.Id)..."
If($RecycleFlag -eq $true)
{
$list.GetItemById($item.ID).Recycle();
}
Else
{
$list.GetItemById($item.ID).Delete();
}
}
$StopTime = Get-Date -Format o
Write-Output "Deleting finished at: $StopTime"
$ElapsedTime = New-TimeSpan -Start $StartTime -End $StopTime
Write-Output "Elapsed time: $ElapsedTime"
Write-Output "Deleted $itemsToDelete items"
}
}