<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Blog posts by Christian Pisani</title><link href="http://world.optimizely.com" /><updated>2021-11-05T07:30:35.0000000Z</updated><id>https://world.optimizely.com/blogs/christian-pisani/</id> <generator uri="http://world.optimizely.com" version="2.0">Optimizely World</generator> <entry><title>Job for deleting removed properties</title><link href="https://world.optimizely.com/blogs/christian-pisani/dates/2021/11/job-for-deleting-removed-properties/" /><id>&lt;p&gt;I&#39;ve been annoyed by properties staying on page types after deleting them from code, especially on the .Net 5 version where &quot;From Code&quot; doesn&#39;t seem to work. I couldn&#39;t find a way to do this, so I wrote this quick and dirty job for removing them. I simply load all properties from optimizely, and check if a property with the same name exists on the type, and if not, delete it.&lt;/p&gt;
&lt;p&gt;It has not been tested thoroughly, and I would definitely not use it in a production environment, but it saves me a lot of annoynance when working locallly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This deletes all data related to the property, so use with caution.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;public class RemovedDeletedPropertiesJob : ScheduledJobBase
    {
        private readonly IContentTypeRepository _contentTypeRepository;
        private readonly IPropertyDefinitionRepository _propertyDefinitionRepository;

        private int _amountDeleted;
        
        public RemovedDeletedPropertiesJob(IContentTypeRepository contentTypeRepository, 
            IPropertyDefinitionRepository propertyDefinitionRepository)
        {
            _contentTypeRepository = contentTypeRepository;
            _propertyDefinitionRepository = propertyDefinitionRepository;
        }
        
        public override string Execute()
        {
            _amountDeleted = 0;
            
            foreach (var contentType in _contentTypeRepository.List())
            {
                var clone = contentType.CreateWritableClone() as ContentType;
                
                if(clone == null) continue;
                
                foreach (var prop in _propertyDefinitionRepository.List(contentType.ID))
                {
                    try
                    {
                        if (contentType.ModelType.GetProperty(prop.Name) == null)
                        {
                            _propertyDefinitionRepository.Delete(prop);
                            _amountDeleted++;
                        }
                    }
                    catch (Exception e) { }
                }
            }

            return $&quot;Removed {_amountDeleted} properties&quot;;
        }&lt;/code&gt;&lt;/pre&gt;</id><updated>2021-11-05T07:30:35.0000000Z</updated><summary type="html">Blog post</summary></entry></feed>