I have been looking into a way to do this.
Before I get into how this can be done, with some problems, I just want to point out that doing this the way that most people would expect it to be done is nearly impossible. If you know anything about the way ACF and WP work, and you’ve looked into this you’ll quickly come to the same conclusion.
Sometimes “impossible” just means very difficult, but in this case it really does mean impossible. WP has limits to what it can do and attempting to have ACF clean up all unused values when they are no longer needed would run into these limits imposed by the way that WP is built. In almost all cases it would simply cause the saving and updating to time out and not complete.
I would love to hear from people that use other custom field plugins an know if any of them clean up after themselves the way people want ACF to clean up after itself. I’d look myself, but I have no interest in these other plugins or seeing what happens with unused data.
I’ve actually started trying to explain why it is impossible several times and failed. Trying to think about what needs to be done to have this automatically happen under every condition nearly makes my head explode.
There is only one way I can think of that this can be done, and even it cannot be made to work under all conditions. That way is to delete all existing data before ACF updates the fields, or to just wholesale delete all content whenever an ACF field is deleted.
The condition where this will not work is altering the location rules of a field group. If a field group is located on posts and you change the location to pages, there simply isn’t any easy way to then remove all of the field values from all of the posts that were already saved. Not without checking every object in WP to discover what objects the fields should not be saved for.
There is also a condition that will cause data that you don’t want deleted to be deleted anyway. This condition will happen with cloned fields and in any other place where a field key is duplicated.
There is also the case of someone deleting fields when the goal is to keep the field data because you want to move the fields into PHP and remove the field group from the ACF admin.
I’ve actually started building a plugin that will do this, but I hesitate do complete it and make it publicly available because there are cases were, if used improperly, people will end up having content deleted that they do not want deleted and hold me responsible for messing up their site. While I know it will work if used properly, I’m afraid that there are probably too ways that it could be used improperly.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!