0

How do I delete all versions from a file except for the last 10?

1 Answer 1

0

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()
}

https://stackoverflow.com/questions/30231132/how-do-i-delete-version-history-files-in-sharepoint-online

1
  • 1
    This 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.