Created
April 6, 2015 19:49
-
-
Save sebastienlevert/76d0e5ac22596cbf6a2a to your computer and use it in GitHub Desktop.
Purges a list from all its content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Purge the content of a SharePoint list or library | |
.PARAMETER List | |
The List Object to purge the content from | |
.PARAMETER BatchSize | |
The BatchSize of the Query to the list | |
.PARAMETER Recycle | |
If this switch is On, the Purge recycles the items instead of deleting them | |
.EXAMPLE | |
Purge-SPOList -Web $web -Identity "Lists/Documents" | |
.EXAMPLE | |
Purge-SPOList -Web $web -Identity db81c710-b4f3-4f39-849e-87f8ed8d7ec6 | |
.EXAMPLE | |
Purge-SPOList -Web $web -Identity "Lists/Documents" -Recycle | |
.EXAMPLE | |
Purge-SPOList -Web $web -Identity "Lists/Documents" -BatchSize 10 -Recycle | |
#> | |
function Purge-SPOList() | |
{ | |
Param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
[Microsoft.SharePoint.Client.List] | |
$List, | |
[Parameter(Mandatory = $false)] | |
[String] | |
$BatchSize = 100, | |
[Parameter(Mandatory = $false)] | |
[switch] | |
$Recycle | |
) | |
Process | |
{ | |
#----------------------------------------------------------------------- | |
# Ensure the ItemCount of the specified List | |
#----------------------------------------------------------------------- | |
if($List.ItemCount -eq $null) | |
{ | |
Load-CSOMProperties -object $List -propertyNames @("ItemCount") -executeQuery | |
} | |
#----------------------------------------------------------------------- | |
# Getting the start ItemCount | |
#----------------------------------------------------------------------- | |
$itemCount = $List.ItemCount | |
#----------------------------------------------------------------------- | |
# While there are results left to delete | |
#----------------------------------------------------------------------- | |
while($itemCount -gt 0) | |
{ | |
[array]$items = Get-ListItems -List $List -BatchSize $BatchSize | |
if ($items.Count -gt 0) | |
{ | |
for ($i = $items.Count - 1; $i -ge 0; $i--) | |
{ | |
#----------------------------------------------------------------------- | |
# If the Recycle switch is on, Recycle the item to the Recycle Bin | |
#----------------------------------------------------------------------- | |
if($Recycle) | |
{ | |
$items[$i].Recycle() | Out-Null | |
} | |
else | |
{ | |
$items[$i].DeleteObject() | |
} | |
} | |
#----------------------------------------------------------------------- | |
# Executes the delete once every batch | |
#----------------------------------------------------------------------- | |
$list.Context.ExecuteQuery() | |
} | |
$itemCount = $itemCount - $items.Count | |
} | |
} | |
} | |
#region Private Methods | |
function Get-ListItems() | |
{ | |
Param( | |
[Parameter(Mandatory=$true)] | |
[Microsoft.SharePoint.Client.List]$List, | |
[Parameter(Mandatory=$true)] | |
[int]$BatchSize | |
) | |
Process | |
{ | |
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery($BatchSize, "ID") | |
$items = $List.GetItems($query) | |
$List.Context.Load($items) | |
$List.Context.ExecuteQuery() | |
return $items | |
} | |
} | |
#endregion | |
. .\Load-CSOMProperties.ps1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment