Home Forums General Issues Flush Unused Custom Fields Reply To: Flush Unused Custom Fields

  • In my list, I’ve basically listed different things in order of difficulty. For example, it is actually pretty easy to delete all of the existing data for a field if a field is deleted.

    When you start getting into the other things, quite honestly, I don’t really have the time to build an application that will root through all the fields in every group and try to find what matches up for something like changing the location of a field group.

    And when it comes to conditional logic, of altering sub fields of repeaters, or even renaming a field, if you want to accomplish this, even if you save other data to help in the process, it means that you need to inspect all of the data that was saved and compare it to all of the data that is saved and I think that this is something that is reasonable to achieve. The amount of work that would be needed to achieve it would far outweigh the benefit of it. To do this you would need to loop through every field group and every field, figure out what should be there and then delete anything that shouldn’t be there and the looping would need to actually look at the conditional logic associated with every field…

    … and then to complicate things even further. Let’s say that you have a tab field and this tab field is controlled with conditional logic. All the fields in the tab are conditional, but they do not need to have their own conditions since the tab controls it. Now, with every field you need to look and see… is this field in a tab? does the tab have conditional logic? should all fields in the tab be removed?

    Like @redvolver says, and I mentioned before, it could be done easily if you just flush everything when a post is saved and make ACF update with new values. The main issue with this method would be clone fields. The reason for this issue is that clone fields causes duplicated field keys. There are some other conditions that could also create duplicate field keys, for example, registering field groups using PHP and using the same field in several places without using a unique key.

    Like I said, I started building something that would do this automatic cleanup, but clone fields stopped me, since, in the end the only thing you really have to go by is the field key. Even a process that must be initiated by an admin would have these problems.

    I’ll continue to think about the issue and if I think of some way to accomplish it without writing a million lines of code to make it safe I will.