0

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

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("userID@yourtenant.com", $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

  • This works, but it ends up deleting every other version. So if I have a document with 110 versions, I'm left with 65. – user1330008 Jun 6 '16 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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