Delete versioning history on Sharepoint online

Copper Contributor

I am trying to fix the following script to check for the version history of a single file to verify that it works. Ultimately, I want this script to check all libraries within our Sharepoint environment and delete any versions older than 20 of the most recent ones. I keep running into an error with either a variable being null or it can't authenticate me. I have the correct level of permission to perform this task. Does anyone have any ideas what I am doing wrong?


# Parameters
$siteUrl = ""
$libraryName = "Documents"
$fileName = "Monthly Tech Audit.xlsx"
$maxVersions = 10

# Connect to SharePoint Online using interactive login
Connect-PnPOnline -Url $siteUrl -UseWebLogin

# Function to delete versions for a specific file
function DeleteFileVersions($file) {
# Get all versions of the file
$versions = Get-PnPFileVersion -ListItem $file.ListItemAllFields

# Delete versions exceeding the maximum number
$versionsToDelete = $versions | Sort-Object -Property Created -Descending | Select-Object -Skip $maxVersions
foreach ($version in $versionsToDelete) {
Remove-PnPFileVersion -ListItem $file.ListItemAllFields -FileVersionId $version.VersionId
Write-Host "Deleted version with ID: $($version.VersionId)"

# Function to perform search using SharePoint Search API
function SearchFile($queryText) {
$searchAPIEndpoint = "/_api/search/query"

$headers = @{
'accept' = 'application/json;odata=nometadata'
'content-type' = 'application/json'

$body = @{
'request' = @{
'Querytext' = $queryText
'RowLimit' = 500
'SelectProperties' = 'Title,Path,IsFolder,ParentLink'
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri "$siteUrl$searchAPIEndpoint" -Headers $headers -Method POST -Body $body

$results = $response.PrimaryQueryResult.RelevantResults.Table.Rows.results | ForEach-Object {
$item = $_.Cells.results | Select-Object -Property Key,Value
[pscustomobject]$item | Add-Member -MemberType NoteProperty -Name $item.Key -Value $item.Value

return $results

# Function to delete versions for each file found
function ProcessSearchResults($results) {
foreach ($result in $results) {
if ($result.Path -ne $null -and $result.Cells -ne $null) {
$filePath = $result.Path
$fileUrl = $siteUrl + $filePath

# Check if the file is in the specified library
if ($filePath.StartsWith("/sites/your-site/$libraryName")) {
$file = Get-PnPFile -Url $fileUrl -ErrorAction SilentlyContinue

if ($file) {
Write-Host "Found file '$fileName' at '$fileUrl'"
DeleteFileVersions $file

# Get the document library URL
$libraryUrl = (Get-PnPList -Identity $libraryName).RootFolder.ServerRelativeUrl

if ($libraryUrl) {
# Perform search for the file within the library
$queryText = "path:'$libraryUrl' AND filename:'$fileName'"
$results = SearchFile $queryText

if ($results) {
ProcessSearchResults $results

Write-Host "Version history cleanup completed."
else {
Write-Host "File '$fileName' not found in the library."
else {
Write-Host "Library '$libraryName' not found."

# Disconnect from SharePoint Online

0 Replies