1
votes

I came across business requirement where I want to delete item from master & web and also delete respective item from solr indexes. business don't want to publish parent item as there might be change i sibling or parent so I cant trigger any strategy like a onPublishEndAsync , onPublishEndAsyncSingleInstance onPublishEndAsyncPreview here I am trying something like below but it is not working

    var ParentItem = item.Parent;
                using (new EditContext(item))
                {
                    if (Sitecore.Configuration.Settings.RecycleBinActive)
                        item.Recycle();
                    else
                        item.Delete();
                }
                   if (item == null)
                return;
            ISearchIndex index = ContentSearchManager.GetIndex((SitecoreIndexableItem)ParentItem );
            if (index == null)
                return;
            index.Refresh(new SitecoreIndexableItem(ParentItem )); or 
           index.RefreshAsync(new SitecoreIndexableItem(item), IndexingOptions.ForcedIndexing, new System.Threading.CancellationToken());

I am not getting any error in sitecore log or solr log files but index is not deleting item

1

1 Answers

1
votes

You can mark your item as unpublishable, publish it (that will remove the item from web database and from web index) and then delete it.

Here is sample code which unpublished item from all publishing target databases:

using (new SecurityDisabler())
{
    var targets = PublishManager.GetPublishingTargets(item.Database)
        .Select(i => Database.GetDatabase(i[FieldIDs.PublishingTargetDatabase]))
        .ToArray();

    var languages = LanguageManager.GetLanguages(item.Database).ToArray();

    item.Editing.BeginEdit();
    item.Publishing.NeverPublish = true;
    item.Editing.EndEdit();

    var handle = PublishManager.PublishItem(item, targets, languages, false, false);
    PublishManager.WaitFor(handle);
}

Just execute item.Recycle or Delete after.

Some time ago I wrote a blog post describing how to add a ribbon button which will "Delete and Unpublish" item with one click. You can find it here:

Automatically unpublish deleted Sitecore item