Items as resources by Sitecore part 3: restoring
When working with Sitecore's items as resources (IAR) feature, it is possible to accidentally overwrite resource items. In part 1 of this blog series I explained how to prevent this from happening, and in part 2 I showed how to generate a report of overwritten items.
However, restoring a resource item and deleting its SQL database version can be tricky. In this blog post, we explore several ways to accomplish this task anyway.
It is important to remember that items stored in resource files cannot be deleted directly. If you have copied an item from a resource file to the database, Sitecore will delete it from the database and restore it from the resource file. So, when I refer to deleting an item in this context, I actually mean restoring the IAR item
Use the CLI cleanup
Dotnet sitecore itemres cleanup
The cleanup subcommand cleans up database items contained in .dat files if the item data in both entries are equal. So this is not useful for restoring IAR items in general, but it is for cleaning up.
Use the Sitecore Content editor
The Sitecore Content Editor is the safest and most common way to delete an overwritten item. When you delete the item in the Content Editor, Sitecore will respond with a message stating that some items have been deleted from the database but restored from resources. The UI will also perform all necessary checks and also delete child items. Sitecore will take care of updating dependencies such as indexes, caches and webhooks, and will place the item in the recycle bin for easy undoing.
However, if you have subitems that are not Items as Resources and should not be deleted, or if you need to delete an in use template item, deleting in the Content Editor may not be entirely feasible. In the case of an in use template item, Sitecore asks you to “Delete all the items that are based on this template first.”
Tip 1: If you want to delete an entire subtree, overwrite the root item and then delete the root and all its subitems. This will restore all overwritten items in that subtree.
Tip 2: If you want to restore a resource item that has non-resource children, package the children first, or move them to the recycle bin. Then delete the overwritten resource item, which will restore it from the resource file. After that, you can retrieve the children by installing the package or restoring them from the recycle bin.
Note: Restoring an Item as Resource triggers both item:delete and item:save events, which can be used for webhooks.
Use Sitecore PowerShell delete
Another way to delete an overwritten Item as Resource is through the use of the Sitecore PowerShell Extensions (SPE). This method is similar to deleting through the Content Editor, but it also allows you to delete an in use template item. The deleted item is stored in the recycle bin, and its sub-items are also deleted. Indexes are updated and events are sent.
Get-Item -Path master: -Query "/sitecore/templates/Feature/Test" |
Use deleteitem from Sitecore SqlServerDataProvider
This option simply deletes the item from the SQL database without triggering events. Caches or indexes are not updated. The use case for this is to remove only the specific item while preserving its child items. However, keep in mind that no webhooks are sent and your index is not updated. Flushing the cache or restarting the server may be required to see the changes to the item.
Combine report and delete
By combining the power of the Sitecore PowerShell report and the ability to delete items, we can make restoring IAR items even more efficient. By using the Sitecore PowerShell report "Items as resources by Sitecore part 2: reports" and Sitecore PowerShell report actions, we can easily select and restore specific items from our resource files. The code for this tool can be found on GitHub, or you can download the report as a resource file and install it. The report includes two delete actions to restore resource items: the PowerShell delete and the SQL delete.
IAR and Sitecore 10.3+
It’s good to know that Sitecore 10.3 introduces new functionality for working with items as resources. This includes an easier way to determine if an item has been overwritten and a method to generate a list of all overwritten items.