London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Delete Orphan Blocks

Vote:
 
Episerver 8 CMS. 
I want to code a scheduled job to delete all the orphan blocks. I created them programatically, all are of the same typeblock. I'm not sure how to select and delete this blocks programatically from a scheduled job.
Regards.
#141267
Nov 10, 2015 17:26
Vote:
 

Hi,

You could try to use this code snippet:

  var contentType = ServiceLocator.Current.GetInstance<IContentTypeRepository>().List().FirstOrDefault(ct=>ct.ModelType == typeof(JumbotronBlock));
            var contentList = ServiceLocator.Current.GetInstance<ContentStore>().ListContentOfContentType(contentType, false).ToList();
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var contentCount = contentList.Count;
            for (int i = 0; i < contentCount; i++)
            {
                var contentUsage = contentList[i];
                contentRepository.Delete(contentUsage.ContentLink, false, AccessLevel.NoAccess);
            }
#141272
Nov 10, 2015 20:56
Vote:
 

Thank you Grzegorz Wiecheć , it worked!!!!!

Regards

#141279
Nov 11, 2015 9:45
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.