11

I am trying to delete a large number of data templates - an entire section. However I have some content items that use those data templates and also there seems to be a lot of template inheritance and Standard Values that use these templates. I wish I could just delete the entire section of the content tree. However when I try to delete the entire section I always get a message telling me that I can't delete a certain template because it is in use. Then I have to hunt down all of the individual content items that use that data template. Once I do that then I can move to the next one. It is a very long and manual process. Is there any way to just delete an entire section of data templates and all of the content items that are based on those data templates?

17

This seems like a perfect taks for Sitecore Powershell Extensions

  1. First of all I would get a list of template IDs to remove

    $templateIds = Get-ChildItem /sitecore/templates/you/better/know/where/to/look -Recurse | 
        ? { $_.TemplateName -eq "Template" }
    
  2. Then you can search your other templates for inheritance

    Get-ChildItem "/sitecore/templates/neverland" - Recurse | ? { $_.TemplateName -eq "Template" } |% {
        $currentTemplate = $_
        $templateIds |% {
            $currentTemplate."__Base Template" = $currentTemplate."__Base Template" -Replace "$($_.ID)\|", ""
            $currentTemplate."__Base Template" = $currentTemplate."__Base Template" -Replace "$($_.ID)", ""
        }
    }
    
  3. Finally for each of your templates you can go over your content and delete items which are based on it

    $templateIds |% {
       $deletingTemplate = $_
       Get-ChildItem "/sitecore/content" -Recurse | ? { $_.TemplateName -eq $deletingTemplate.Name } | Remove-Item
    }
    

This is a rough idea of how this can be done with SPE. You might need to tweak it here or there. Ok, fixed the code a bit. There still might be some small issues but it should moreover be correct.

Remember though! Never run untested powershell scrips on production and always make a backup - this code can potentially damage your data!

  • 2
    that's exactly the code that I didn't write ;) – Bas Lijten Oct 5 '16 at 21:21
5

I don't have a "one size fits all solution, but I can give you some advice:

  • investigate what data templates you need to delete
  • Create a Sitecore powershell script (or some c# code) which will iterate through all your sitecore items, check for their templates and remove them if you got a match.

This kind of automations is the only way (as far as I know) to save you loads of time

4

Not a direct solution for your problem, but if any other scenarios,

if you want to delete an item without respecting the references to other items, you can use /sitecore/admin/DbBrowser.aspx tool.

Note that this tool will skip more of the pipeline processers and should be used cautiously.

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.