How do I delete all versions from a file except for the last 10?
1 Answer
check this one
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$password = Read-Host -Prompt "Enter password" -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("[email protected]", $password)
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursitecollection"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$context.Credentials = $credentials
$fileUrl = "/sites/yoursitecollection/path_to_file/filename";
$versions = $context.Web.GetFileByServerRelativeUrl($fileUrl).Versions;
$context.Load($versions)
$context.ExecuteQuery()
for($i=10;$i -lt $versions.Count-10; $i++)
{
$versions[$i].DeleteObject()
$context.ExecuteQuery()
}
-
1This works, but it ends up deleting every other version. So if I have a document with 110 versions, I'm left with 65. Commented Jun 6, 2016 at 13:45