Home › Forums › Backend Issues (wp-admin) › How do I clean up old ACF fields in DB? › Reply To: How do I clean up old ACF fields in DB?
I’m not entirely sure this would work, however, if someone is adventurous and has some time to kill they could. This is just a general outline.
Create an acf/save_post function.
In this function get all of the postmeta fields in the database for the post. You’d want to specifically get all fields with a value like ‘field_%’. The meta_key values of these fields in the database are you field name pre_pended with an underscore.
You could then loop through all of the posted data that is posted using the acf field key values (‘field_%’) and compare them to the list of fields that you collected in the first step, removing from your list anything that’s being submitted. This would be the hard part because the posted data will be a complex nested array.
What you should have left is a list of fields that are no longer used.
loop trough the this list and delete all of the unused fields ‘MY_FIELD’ & ‘_MY_FIELD’ using delete_post_met();
If you could get it to work it might be a useful for others.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.