After the upgrade Sitecore platform from 9.3 to 10.2 along with all compatible modules, one of our Content Author comes to me with an issue. The issue was the publishing of SXA web sites was bombing out with an error message.
The Issue
At the surface the issue was, the Publishing of SXA website was throwing below error.

Job started: Publish to 'web'|#Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AggregateException: One or more exceptions occurred while processing the subscribers to the 'item:deleting' event. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Bynder.SitecoreConnector.Extensions.TemplateExtensions.IsDerived(Template template, ID templateId)
at Bynder.SitecoreConnector.EventHandlers.BynderMediaItemEventHandler.OnItemDeleting(Object sender, EventArgs args)
at Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result)
--- End of inner exception stack trace ---
at Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result)
at Sitecore.Events.Event.RaiseEvent(String eventName, Object[] parameters)
at Sitecore.Events.Event.RaiseItemDeleting(Object sender, ItemDeletingEventArgs args)
at Sitecore.Events.Event.DataEngine_ItemDeleting(Object sender, ExecutingEventArgs`1 e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Sitecore.Data.Engines.EngineCommand`2.RaiseEvent[TArgs](EventHandler`1 handlers, Func`2 argsCreator)
at Sitecore.Data.Engines.EngineCommand`2.RaiseExecuting(Boolean& cancelled)
at Sitecore.Data.Engines.EngineCommand`2.CanExecute()
at Sitecore.Data.Engines.EngineCommand`2.Execute()
at Sitecore.Data.Engines.DataEngine.DeleteItem(Item item)
at Sitecore.Publishing.PublishHelper.DeleteTargetItem(ID itemId)
at Sitecore.Publishing.Pipelines.PublishItem.PerformAction.ExecuteAction(PublishItemContext context)
at Sitecore.Publishing.Pipelines.PublishItem.PerformAction.Process(PublishItemContext context)
at (Object , Object )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Publishing.Pipelines.PublishItem.PublishItemPipeline.Run(PublishItemContext context)
at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context, List`1& referrers, List`1& children)
at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.Process(PublishContext context)
at (Object , Object )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Publishing.Pipelines.Publish.PublishPipeline.Run(PublishContext context)
at Sitecore.Publishing.Publisher.PublishWithResult()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)
at Sitecore.Jobs.JobRunner.RunMethod(JobArgs args)
at (Object , Object )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Jobs.DefaultJob.DoExecute()
at Sitecore.Abstractions.BaseJob.ThreadEntry(Object state)
The solution
From above error we were not getting any fruitful information. So we turned to Logs. In the logs we have an error logged as below
2224 02:07:01 ERROR Data template '{7F9D1A45-F31E-4714-AC66-1E300AE1B792}' not found for item '/sitecore/content/Brands/Base/Base Website/Data/Forms' in 'web' database
This is now clear indication that the template item is missing but the actual item created out of that template is still exist. We also get the Forms item is under the Data folder which suppose to be SXA’s module item. It is not the item of type custom template. In other words, this is OOTB SXA module item and the missing template should also be part of OOTB XSA module.
I taked to one of my colleague about this issue and he vaguely remember he read something about it somewhere. He find that blog for me here.
The blog have that detailed under Issues after the SXA upgrade section and that blog is missing the script to remove the orphan items.
Here is the simple script we put together along with my colleague for removing Forms item under the data folder of all SXA websites under the Sitecore/Content tree bases on the template id {7F9D1A45-F31E-4714-AC66-1E300AE1B792}
New-UsingBlock (New-Object Sitecore.Data.Events.EventDisabler) {
Get-Item -Path "master:" -Query "/sitecore/content//*[@@templateid='{7F9D1A45-F31E-4714-AC66-1E300AE1B792}']" | Remove-Item
Get-Item -Path "web:" -Query "/sitecore/content//*[@@templateid='{7F9D1A45-F31E-4714-AC66-1E300AE1B792}']" | Remove-Item
}
As you may have noticed, we wrapped the removal script within event disabler. So that the deletion operation succeed, otherwise it will fail with similar error message as above.
If you have additional Publishing target other than web, the script needs to extent/alter to remove items form that database as well.
Note