views:

324

answers:

1

I have the following structure in my Sitecore media library

images/department/sub-department/product/

And I want to delete all of the images in code a department at a time. At the moment I have

Item[] items = database.SelectItems("/sitecore/media library/images/department1//*");            

Sitecore.Data.Engines.DataEngine engine = new Sitecore.Data.Engines.DataEngine(database);

using (new SecurityDisabler())
{
    foreach (Item item in items)
    {
        engine.DeleteItem(item))
    }
}

The problem is it only deletes the child images of department1 and nothing underneath that?

+1  A: 

DataEngine is a very low level API, which shouldn't be used without a reason.

Deleting all subitems simultaneously is easy:

Item department1 = database.GetItem("/sitecore/media library/images/department1"); department1.DeleteChildren();

This will leave department1 item, but everything under it will be deleted.

Alexey Rusakov
Thank you for your answer, what kind of reason would you use the DataEngine for?
Nick Allen - Tungle139
Quick answer is never, unless you know what you're doing.Longer answer is that sometimes people need a hardcore customization or a workaround, that cannot be achieved using higher level, public APIs. But even then, it's likely to be done by Sitecore support stuff, this is not something we document
Alexey Rusakov