1

Our company SharePoint site got into a loop of a script which duplicated all the files in the libraries from our list. Now we are stuck with 50+ versions of each files and depleted our 1TB in Sharepoint Online. We need to delete all the versions that the sharepoint admin account created.

Thanks

Update: We are on SharePoint Online. We have two big lists. One with all the name of the compagnies with a link that filters all the mandates from the company. The second list, is all our mandates name with a column with a link that goes towards the mandate library. All of our library starts with "Mandat...".

I would like to scan all those libraries, find the documents inside and then delete the last 10 versions of that author called SharePoint Admin.

1
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

$User = Read-Host -Prompt "Enter User" -AsSecureString
$password = Read-Host -Prompt "Enter password" -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $password)

$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursitecollection"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$context.Credentials = $credentials


$list = $context.Web.Lists.GetByTitle("Documents");
$context.Load($list)
$context.ExecuteQuery()


    $folder = $list.RootFolder;
    $context.Load($folder);
    $context.ExecuteQuery()


    $context.Load($folder.Files);
    $context.ExecuteQuery()

    foreach ($f in $folder.Files)
    {
    $versions = $f.Versions;

    $context.Load($versions)
    $context.ExecuteQuery()
    $count=0;
    for($i=10;$i -lt $versions.Count; $i++) 
    {

            if($versions[$i].CreatedBy.User.DisplayName=='System account')
            {
                 if($count<10)
                 {
                     $count++;
                     $versions[$i].DeleteObject()
                     $context.ExecuteQuery()
                 }
            }

    }
    }
  • That's awesome. Let's say there's documents with 20+ versions of same author, will it delete all versions and document? or leave it to one version only? Is it possible to add and delete lets' say the last 10 versions of the document by the author? – Fr4nk Feb 7 '18 at 21:02
  • It will delete all the versions. I have updated the code to limit the deletion to 10 versions. – Deepmala Feb 8 '18 at 3:32
  • It didn't work. – Fr4nk Feb 14 '18 at 17:26

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.