World is now on Opti ID! Learn more

Christian Thorvik
Nov 5, 2021
  1634
(1 votes)

Job for deleting removed properties

I've been annoyed by properties staying on page types after deleting them from code, especially on the .Net 5 version where "From Code" doesn't seem to work. I couldn'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.

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.

This deletes all data related to the property, so use with caution.

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 $"Removed {_amountDeleted} properties";
        }
Nov 05, 2021

Comments

Per Nergård (MVP)
Per Nergård (MVP) Nov 5, 2021 12:52 PM

I did this MissingProperties initmodule/plugin a long tme ago but the logic in that for identifying missing properties does not work in :Net Core version?

 https://world.optimizely.com/blogs/Per-Nergard/Dates/2016/4/missing-properties-initializationmodule/

Christian Thorvik
Christian Thorvik Nov 5, 2021 01:21 PM

I only found the old blog post where the code is missing, so I haven't seen this one. It seems to work though. Only the appsettings part doesn't work, but that's simple enough to fix

Per Nergård (MVP)
Per Nergård (MVP) Nov 5, 2021 01:41 PM

NIce!


Josh Salwen
Josh Salwen Mar 15, 2022 11:51 PM

This works perfectly!

Please login to comment.
Latest blogs
Optimizely Frontend Hosting: Deploy Without PowerShell Using the @kunalshetye/opticloud Package

In my last two blog posts, I walked through how to get started with deploying a headless app to Optimizely Frontend Hosting using PowerShell and th...

Szymon Uryga | Jul 15, 2025

New Administrator Certifications Are Here — 7 Ways to Get Certified with Optimizely

Not a developer but want to prove your product expertise? We’ve got great news. We’re thrilled to announce the expansion of Optimizely’s...

Satata Satez | Jul 15, 2025

Optimizely London Dev Meetup 10th July 2025

Overview & Agenda As 2025 rolled around this year it was time for another one of our much beloved developer meetups in London. This year, I took us...

Scott Reed | Jul 15, 2025

Optimizely Developer Meet-up (for Non-Techies)

I’ve been part of the Optimizely community for over seven years. Back when Episerver was still just “Epi”, Optimizely was the best A/B testing...

Mark Welland | Jul 14, 2025