Remove invalid retention policy from SharePoint Online

Sven LüdersSven Lüders
3 min read

Despite the common sense of using retention features in Microsoft 365 I never had trouble removing data if I had to. But last week I faced something new about invalid retention policies - but lets start from the beginning.

As we all know, sometimes things do not work as expected…in this case somebody uploaded a bunch of files resulting in extensive SharePoint storage usage. And of course the data has been deleted, but the site is configured for retention.

My general approach of deleting data, which is on hold is more or less the same:

  1. If there are Retention Policies in place, exclude the site or group from the retention policy.

  2. Close existing eDiscovery cases with a hold

  3. Disable Data loss prevention (DLP) policies, if there are some

In my specific case I double-checked all parts of the game, but only had to set the retention policy exclusion. Five minutes later (after the policy status showed success) I browsed the Preservation Hold Library. Important to mention that this library keeps copies of all modified or deleted files as enforced by the Retention policy or eDiscovery holds. You can reach it by using “PreservationHoldLibrary“ as a suffix on your SharePoint site URL: https://TenantDomain.sharepoint.com/sites/SiteName/PreservationHoldLibrary

I tried to delete some files…and tried and tried. Every M365 admin knows that 5 minutes is not the appropriate period of time to get things changed. So I kept trying after 24 hours… But the error still told me very kind:

“Request was cancelled by event received. If attempting to delete a non-empty folder, it’s possible that it’s on hold.”

Of course the status of the retention policy was showing success and not pending. So I assumed the group site is excluded. I also tried to get insights with PowerShell and used the Invoke-HoldRemovalAction cmdlet to find applied holds. But there were no holds applied, so I still had to search for them:

The version history was still showing that the files are not expiring because they are on hold:

During a helpful Microsoft support case a guy told me to use the “Help & support“ in the M365 Admin Center to find and remove invalid retention policies by searching for: Remove invalid retention policy from SharePoint or OneDrive

Just run the test and magic happens: it shows the invalid policy, which still applies:

Just acknowledge and update in the wizard and there you go 🪄 Now I was ready to delete files and so I let it be, let it be, let it be, let it be… 🎶

Wow! Lesson learned - this thing is actually good for something!

And just in case you have to delete a lot of files use PowerShell with the PNP module.

$siteURL = "https://yourtenant.sharepoint.com/sites/yoursite"
$listName = "Preservation Hold Library"

# install PNP module [https://pnp.github.io/powershell/articles/installation.html]
Install-Module PnP.PowerShell -Scope CurrentUser

# register EntraID app for PnP PowerShell interactive login approach [https://pnp.github.io/powershell/articles/registerapplication.html]
Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP.App" -Tenant yourtenant.onmicrosoft.com -Interactive

# connect to SPO with PNP
Connect-PnPOnline -Url $siteURL -Interactive -ClientId <client id of your Entra ID Application Registration>

# get list
$list = Get-PnPList -Identity $listName 

# retrieves all list items from the list in pages of 1000 items and delete each item
Get-PnPListItem -List $list -PageSize 1000 -ScriptBlock {
        Param($items)
        Invoke-PnPQuery 
        } | ForEach-Object {
            # remove files without confirmation
            Remove-PnPListItem -Identity $_ -Force
        }

Time for a shout-out for my favorite site about SharePoint is https://www.sharepointdiary.com. Finally don’t forget to remove the exclude setting in the retention policy afterwards 😉

1
Subscribe to my newsletter

Read articles from Sven Lüders directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sven Lüders
Sven Lüders