Delete orphaned temporary pages from Site Pages
Script to clean up temporary pages from Site Pages library
Summary
Locate, take ownership of, and delete temporary orphaned pages from /SitePages library.
$SiteURL = "https://{tenant}.sharepoint.com/sites/{site}"
$ListName = "Site Pages"
Disconnect-PnPOnline
#use a login with Site Collection Administrator or Owner permissions
Connect-PnPOnline -Url $SiteURL -Interactive
$context = Get-PnPContext
$list = Get-PnPList -Identity $ListName
$context.Load($list)
Invoke-PnPQuery
$checkedOutFiles = $list.GetCheckedOutFiles()
$context.Load($checkedOutFiles)
Invoke-PnPQuery
$filesProcessed = @()
foreach($checkedOutFile in $CheckedOutFiles) {
$fileServerRelativeUrl = [string]::Concat($list.ParentWebUrl, $checkedOutFile.ServerRelativePath.DecodedUrl.Replace($list.ParentWebUrl, ""))
#8 random characters seem to be used for temporary page names. Be careful - if a valid page was created with valid 8 character name, that page would be deleted as well. TODO: figure out a way to avoid valid pages?
$tempPageSampleUrlForThisSite = $list.RootFolder.ServerRelativeUrl + "/zz5yfe8u.aspx"
if ($fileServerRelativeUrl.Length -ne $tempPageSampleUrlForThisSite.Length) {
Write-Host "Skipping $fileServerRelativeUrl" -ForegroundColor Green
} else {
$checkedOutFile.TakeOverCheckOut()
Invoke-PnPQuery
$file = $checkedOutFile.Context.Web.GetFileByServerRelativeUrl($fileServerRelativeUrl)
$context.Load($file)
Invoke-PnPQuery
$filesProcessed += [PSCustomObject]@{
ServerRelativeUrl = $fileServerRelativeUrl
Exists = $file.Exists
UIVersionLabel = $file.UIVersionLabel
}
$file.DeleteObject()
$context.ExecuteQuery()
Write-host "." -NoNewline
}
}
$nowString = [System.DateTime]::Now.ToString("yyyyMMdd_hhmm")
$path = "$env:USERPROFILE\Desktop\PS1\Get-FilesWithNoCheckin_$nowString.csv"
$filesProcessed | select ServerRelativeUrl, Exists, UIVersionLabel | Export-Csv $path -NoTypeInformation
$SiteURL = "https://{tenant}.sharepoint.com/sites/{site}"
$listName = "Site Pages"
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
Write-Host "Logging in the User!"
m365 login --authType browser
}
$filesProcessed = @()
$pages = m365 spo page list -u $SiteURL | ForEach-Object { $_.replace("Id", "_Id") } | ConvertFrom-Json
$list = m365 spo list get -u $SiteURL -t $listName | ConvertFrom-Json
$checkedOutPages = $pages | ? CheckOutType -eq 0
foreach ($page in $checkedOutPages){
$fileServerRelativeUrl = [string]::Concat($list.ParentWebUrl, $page.ServerRelativePath.DecodedUrl.Replace($list.ParentWebUrl, ""))
#8 random characters seem to be used for temporary page names. Be careful - if a valid page was created with valid 8 character name, that page would be deleted as well. TODO: figure out a way to avoid valid pages?
$tempPageSampleUrlForThisSite = $list.RootFolder.ServerRelativeUrl + "/zz5yfe8u.aspx"
if ($fileServerRelativeUrl.Length -ne $tempPageSampleUrlForThisSite.Length) {
Write-Host "Skipping $fileServerRelativeUrl" -ForegroundColor Green
} else {
if(!$page.IsPageCheckedOutToCurrentUser){
#take over check out
m365 spo file checkin --webUrl $SiteUrl --fileUrl $page.ServerRelativeUrl
m365 spo file checkout --webUrl $SiteUrl --fileUrl $page.ServerRelativeUrl
}
$filesProcessed += [PSCustomObject]@{
ServerRelativeUrl = $page.ServerRelativeUrl
Exists = $page.Exists
UIVersionLabel = $page.UIVersionLabel
}
m365 spo page remove -u $SiteURL -n $page.Name --confirm
}
}
$nowString = [System.DateTime]::Now.ToString("yyyyMMdd_hhmm")
$path = "$env:USERPROFILE\Desktop\PS1\Get-FilesWithNoCheckin_$nowString.csv"
$filesProcessed | select ServerRelativeUrl, Exists, UIVersionLabel | Export-Csv $path -NoTypeInformation
## Disconnect the context
m365 logout Contributors
| Author(s) |
|---|
| Brian P. McCullough |
| Reshmee Auckloo |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.