Deleting Child Items

Deleting Child Items

Sitecore.Configuration.Factory

  • Class that encapsulates the general information available in the web.config file
  • Contains methods that provide access to this information and the means to override it
  • Sitecore.Collections.Childlist is faster than item.Children property

Deleting Child Items Steps

  1. Use a security disabler to allow changes and place in a using block to ensure cleanup occurs
  2. Create an object for the database where the item is located - replace SitecoreDB with the name of the Sitecore database where you want to perform the update, typically “master”
  3. Create an object for the parent item in the database - replace parentpath with the absolute path to the parent item in the Content Tree, such as “/sitecore/content/home/about”
  4. Assign the collection of child items of the target Item to an object
  5. Enumerate through the collection of child items
  6. Delete each child item from the database


C# Web Control Code

using (new Sitecore.SecurityModel.SecurityDisabler())
{

      Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

      Sitecore.Data.Items.Item item = db.GetItem("parentpath");

      Sitecore.Data.Collections.ChildList children = item.Children;

      foreach (Item child in children ) 
      {
             child.Delete();
      }
}