Disable Sharepoint and Onedrive version history and delete old versions to clear space

Disable Sharepoint and Onedrive version history and delete old versions to clear space

If you are running out of capacity on your Onedrive, even though the files you are using do not hit the capacity limit the issue may be because Onedrive stores 500 version of files.

If you are working on large files and doing lots of edits, a 200mb file can rapidly consume 15Gb or more due to hundreds of versions being stored.

First disable version history.

Once this is complete login to the users Onedrive, go to the settings gear on the top right, Onedrive settings

  • Edit the address to change onedrive.aspx at the end of the address to to viewlsts.aspx e.g. https://mydomain-my.sharepoint.com/personal/mylogin_com_au/_layouts/15/viewlsts.aspx
  • Open the Settings gear from the top right again and open Library Settings

  • Open versioning settings. You can now set the radio button to No versioning. 

  • Press OK to save the setting. This will prevent new documents from creating versions, but old documents will still have versions.
  • You can now go to individual files, click on the vertical … to th eright of the file name and select versions then delete them. However this is cumbersome to do file by file.
  • There is a script to delete all old versions below, but Microsoft in their wisdom have a 5000 file limit on its operation, so if you try to run this on more than 5000 files you  will get the message The number of items in this list exceeds the list view threshold. See more on this issue https://docs.microsoft.com/en-us/sharepoint/troubleshoot/lists-and-libraries/items-exceeds-list-view-threshold
  • Replacing mydomain and my login with the relevant onedrive folder path for the user.

#Config Parameters
$SiteURL= “https://mydomain-my.sharepoint.com/personal/mylogin_com_au
$LibraryName=”Documents”

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Get the web and Library
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($LibraryName)

#Get all List items from the library – Exclude “Folder” objects
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml=”<View Scope=’RecursiveAll’><Query><Where><Eq><FieldRef Name=’FSObjType’/><Value Type=’Integer’>0</Value></Eq></Where></Query></View>”
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

#Loop through each file in the library
Foreach($Item in $ListItems)
{
#Get all versions of the file
$Ctx.Load($Item.File)
$Ctx.Load($Item.File.Versions)
$Ctx.ExecuteQuery()
Write-host “Processing Item:” $Item.file.Name

#Delete all versions of the file
If($Item.File.Versions.count -gt 0)
{
$Item.File.Versions.DeleteAll()
$Ctx.ExecuteQuery()
Write-host -f Green “All Versions deleted on “$Item.file.Name
}
}
}
Catch {
write-host -f Red “Error Deleting version History!” $_.Exception.Message
}