Removing and organizing fields in the content

When you delete a field in a blueprint it remains in the content even if you save the page again.

Is there a way to remove the fields that no longer exist from the content, other than manually?

And also a way to organize the fields in the content with the same order than in the blueprint when there has been changes?

2 Likes

Guess you could write a script that reads the data and then writes it to file again ($page->update()), setting fields that no longer exists to null if you really need such a cleanup. The order of the fields in the content file doesn’t matter though, why do you need to change it?

It’s amazing how fast you get answers in this forum.

Could you give me some more details about that script?

Regarding the second question, It’s just I’m an order maniac :wink:

I don’t have a script to share, but basically you would

  • loop through the pages you want to clean up
  • read the data from all fields into an array in the right sort order, the field you want to delete must be a key with value null; this can be done with a loop through all fields ($page->content()->fields())
  • update the the page with the resulting data array

Ok, I will try it.

Thanks Sonja

Better do your tests on a copy of your project in case anything goes wrong :wink:. And if you get stuck, come back here…