0

This has been discussed many times and here is another one much loved Content Type Hub issue. :)

So, in SPO I'll have some Content Types in the Content Type Hub that cannot been deleted. In the SPO Admin Center when trying to delete a Content Type is says that it cannot be deleted as it is in use.

I have tried:

  • I have tried to publish the CT, wait for +8h and then unpublish it and wait another 4-8h before trying to delete it.

  • Have emptied the primary and secondary recycle bin for each site in the tenant using PnP: Clear-PnPRecycleBinItem -All -Force

  • Via code trying to find the content type and having it removed. Below code is currently saying that the CT with a specific ID is not found on any site.


    function FindContentTypeById {
        param (
            [Parameter(Mandatory=$true)]
            [string]$ContentTypeId        
        )
        
        $siteCollections = GetSites ## Get all sites in the tenant custom function
        
        foreach ($site in $siteCollections) 
        {
            $siteUrl = $($site.Url)
            Write-Host "+ Site: $siteUrl"
            ConnectPnPOnline -Url $siteUrl
    
            $lists = Get-PnPList
            foreach ($list in $lists) 
            {
                $ct = Get-PnPContentType -List $($list.Title) -Identity $ContentTypeId -ErrorAction SilentlyContinue
                if($null -ne $ct) {
                    Write-Host -ForegroundColor Yellow "    - Found CT"
                    Write-Host -ForegroundColor Yellow "      - Found: $($ct.Name) in List: $($list.Title)"
                }
            }
    
            $siteCTs = Get-PnPContentType
    
            $x = $siteCTs | Where-Object {(($_.Id).StringValue -eq ($ContentTypeId))}
            if($null -ne $x)
            {
                Write-Host "  - Found: $($x.Name) in ($site.Url)"
            }
    
            Disconnect-PnPOnline
        }
    }

I have also run code that looks for a Content Type by Name with the same result (not found).

So, any ideas what else I could do/try to have the CT deleted?

1

Please try to run following powershell to find if the content type has been used(remember to change the parameter):

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Get-ContentTypeUsage()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $ReportFile       
    )
 
    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        Write-host "Processing Site:"$SiteURL
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
     
        #Get the content type from web
        $ContentTypeColl = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
         
        #Array to hold result
        $Results = @()
 
        #Get all lists and libraries from the site
        $Ctx.Load($ctx.Web.Lists)
        $Ctx.Load($Ctx.Web.Webs)
        $Ctx.ExecuteQuery()
 
        Foreach($List in $Ctx.Web.Lists)
        {
            $Ctx.Load($List.ContentTypes)
            $Ctx.ExecuteQuery()
        
            #Check if the content type exists in the list       
            $ContentType = $List.ContentTypes | Where {$_.Name -eq $ContentTypeName}
            If($ContentType -ne $Null)
            {
                $Results += New-Object PSObject -Property @{
                'Site' = $SiteURL
                'List Name' = $List.Title
                'URL'= $SiteURL+$List.DefaultViewURL
                }
                Write-host "Found Content Type Reference at '$($List.Title)' in site '$SiteURL'" -f Green
            }
        }
        #Export Results to CSV
        $Results | Export-Csv $ReportFile -Append -NoTypeInformation
 
        #Process subsites - Call the function recursively
        If($Ctx.Web.Webs.Count -gt 0)
        {
            Foreach ($web in $Ctx.Web.Webs)
            {
                Get-ContentTypeUsage -SiteURL $web.Url -ContentTypeName $ContentTypeName -ReportFile $ReportFile
            }
        }
  }
    Catch {
        write-host -f Red "Error Deleting Content Type!" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ContentTypeName="Project Template"
$ReportFile="C:\Temp\ContentTypeRef.csv"
 
#Delete the Output report file if exists
if (Test-Path $ReportFile) { Remove-Item $ReportFile }
 
#Call the function
Get-ContentTypeUsage -SiteURL $SiteURL -ContentTypeName $ContentTypeName -ReportFile $ReportFile

If the content type is not found, we suggest you raise a ticket to Microsoft to check if there is something wrong on back-end side.

More inforamtion:

https://www.sharepointdiary.com/2016/12/fix-another-site-or-list-is-still-using-this-content-type-error.html

3
  • Thanks for your reply and I do actually have an MS ticket open where they actually sent the script which you refer to and which I had to modify so it parses all sites in the tenant. The result of the script is the same as mine above; nothing found. Thanks though.
    – Adam
    Jan 26 at 10:22
  • If there is any update from Microsoft, please feel free to reply.It will do great help to those who meet the similar question in this forum.
    – Julie_MSFT
    Jan 27 at 8:58
  • I will let you know for sure but so far no updates. I'm not that hopeful that I will get a solution unless my ticket is sent to someone on the "inside". One purpose with the ticket has been to get some attention that the CTH need improvements on the admin side. We'll see what happens but I will update this thread.
    – Adam
    Jan 27 at 17:30
0

So I have finally been able to delete the "undeletable" content types. I did a couple of things after each other hence I cannot tell for sure which of the actions it was. I could however argue that most certainly it was deleting ALL sites in the “Deleted Sites” section in the SharePoint Admin using: Get-SPODeletedSite | Remove-SPODeletedSite . I really wanted to avoid this but today I gave up.

Besides deleting all the deleted sites I once again ran a new recycle bin clean up of all active sites using:

function EmptyRecycleBin {
    $siteCollections = Get-PnPTenantSite    
    foreach ($site in $siteCollections) 
    {
        Write-Host "+ Cleaning RecycleBin on site: $($site.Url)"
        ConnectPnPOnline -Url $($site.Url)
        Clear-PnPRecycleBinItem -All -Force
        Disconnect-PnPOnline
    }
}

Finally, I noticed that "Get-PnPTenantSite" don't return the Content Type Hub site (/sites/contenttypehub) so I connected to the Content Type Hub site manually and cleared the recycle bin.

After doing the above I could delete my content types from the Content type gallery.

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.