Support

Account

Home Forums General Issues Flush Unused Custom Fields

Solved

Flush Unused Custom Fields

  • recently i also came across this problem. i don’t mind the unused data per se but that it turns up in search results is a bummer. a lot of wordpress sites seem to suffer from this since i find a lot of weird search results on various wordpress sites i experimented with. 🙂

    what is the current status of this and the cleanup plugins that some users did?

    wouldn’t a simple solution be to just delete all custom fields of a post on save_post (priority 1) and acf will store the currently used ones again afterwards anyway? a batch job could walk through all posts and resave them.

    someone mentioned a potential issue with clone fields but i never used them and don’t even know what they are. 🙂

  • This is a pretty heavy-handed ‘solution’ but, for what it’s worth, what I’m doing at the moment to remove unused ACF post_meta data is this…

    I name all my ACF fields with a unique prefix (eg. my_prefix_first_name, my_prefix_last_name, etc…) making sure that my_prefix is unlikely to conflict with any default WordPress post_meta keys or any of the post_meta keys created by plugins that I might use.

    Then, every time I update a post, I simply delete ALL post_meta containing my_prefix using the acf/save_post action below:

    function acf_delete_all_post_meta($post_id) {
        global $wpdb;
        $result = $wpdb->query($wpdb->prepare("
            DELETE FROM wp_postmeta
            WHERE meta_key
            LIKE '%my_prefix%'
            AND wp_postmeta.post_id = %s",
            $post_id
        ));
    }
    add_action('acf/save_post', 'acf_delete_all_post_meta', 1);

    With a priority of less than 10, this action will fire BEFORE the actual post data is saved. Thus, when the post data IS saved, it’s saved to a clean slate and only saves post_meta for fields that are currently being used in the post.

    Obviously, this technique will not flush your entire site of unwanted post_meta in one go – it only acts upon one post at a time and only when you update the post. However, if used during development (which is probably the only time I would use it) it will help tidy-things-up as you go along.

  • How would you modify this function for a multi-site? I do know the Site ID and the site-specific tables in which I want to delete left over acf meta data

  • +1

    No one likes a mess. And unless you get it right the first time (who does that?) you get a mess. A big messy mess. Seems like enough folks want this. Lot’s of paying custys. Put this at top priority finally after all these years.

    For anyone still interested, HookTurn has an awesome plugin that totally reworks how ACF stores data. Makes cleanup a breeze but the plugin is pricey!

    https://hookturn.io/downloads/acf-custom-database-tables/

  • Hi,

    We are still waiting. It’s been 5 years since the functionality request 🤦,

    Rodrigo

  • +1

    All I need is a tool to delete orphaned ACF meta data. When you’re creating a site you frequently rename fields that have data already. It would be nice to just have a button that removes ACF meta data that’s no longer part of a field group.

  • I’m wondering if this functionality has (seemingly) been avoided due to the expensive nature of doing all of the compares necessary in posts and meta, to ensure nothing is being wiped that’s still in use.

    Perhaps it’s not as straightforward as it seems. Nonetheless, should still be called out as such if that’s the blocker.

  • +1

    There could be at least a warning to developers that an old custom field with the same key and post ID will show up on front end.

    It seems that flushing by field group should be helpful and feasible. If there is a field group ID than all the data with that ID could be deleted.
    Or any non active…
    One would think that if a value was set to a field key, which is in a non-active field group, it shouldn’t show up in query’s….

    As I am right now, I wouldn’t mind deleting all my custom field values and setting the ones that I need again.
    Anyone knows how to do this? I didn’t want to go messing directly with the DB.

    This plugin is amazing, but this… I just wish I knew about this…

  • @mateusvitor As you can see we’ve been asking about this for YEARS now. I’m still astounded that devs of a professional plugin like this allow it to KNOWINGLY leave behind no longer needed data in the DB!! I thought one of the pillars to code development was to “clean up your mess”. You delete can delete all created custom fields.. and there is still data left behind in DB. You can even delete the plugin.. and there is still data left behind in DB.

  • @mateusvitor

    It seems that flushing by field group should be helpful and feasible. If there is a field group ID than all the data with that ID could be deleted.

    Data is stored in the postmeta table. This data is associated with the post ID of the post the data is assocaited with. There is nothing in the way that WP is build to also to associate this content with the field group the field belongs to.

    The number of queries that would be involve to track down and delete all data when a field is deleted would in all likelihood time out the process. I’m sure that the reason that a solution has not been is not because the developer does not want to provide a solution but because there isn’t a practical way to supply that solution working within the way that WP and the DB are built.

  • So, basically the plugin creates data in the DB that 1. itself won’t remove, 2. provides no clear cut way for us to remove, 3. will endlessly create DB bloat anytime an entry is created / deleted including testing creations?

  • ACF does not create data in the database that it can’t remove. ACF creates an easy to use admin that allows data to be entered. These fields are not in any way different than using the built in custom fields that are provided by WP if you enable the feature. Anything entered here will also never be deleted if you decide not to use the fields again at some future time, that is unless you go into the DB and track it all down to delete it. ACF does not do anything that is not possible without using it, it just makes it easier to use what is already there.

    The only time data is left behind is if you 1) delete a field 2) rename a field 3) delete a field group. Any data previously entered into fields in one of these conditions will linger until the post is deleted.

    When I develop something, when I’m testing something that I may not use, or under any condition that I think I may need to alter or delete fields, I do this work on a development site. Even in the cases where the dev site will become the live site I use temporary posts that I create and these temporary posts are all deleted before live content is entered and this removes any data I may have needed to enter while testing.

    If I find that I must alter a field after a site is live and it contains live data then I try to use the same field name. Depending on the alteration I add logic to my template code that to deal with both the old field and new field if at all possible. If I must delete a field I make a decision at that point if I need that data removed, but the chances of me needing to do this are small because of the way that I do my work.

    I agree, it would be nice if this data was cleaned up, unfortunately, as I said, because of the way that that WP and the DB are built it is not an easy task to accomplish unless a significant amount or forethought is applied.

  • Hey, I have a follow up question.
    I will be reuploading all data to my website.
    Can you help me with some SQL to clean, say, all values where a Specific field is associated with a specific post?

    Or even to clean everything to slate (not the fields, just the values).

    Please.

  • +1 for this feature. One of the biggest issues I’m running into with ACF is a bloated database at the moment, for this reason but also tons of empty entries being saved to the DB.

  • For those of you still struggling with this problem, I’ve found a super light and easy-to-use plugin that works like charm. It’s called Edit Custom Fields: https://wordpress.org/plugins/edit-custom-fields/. It lists all custom fields DB entries automatically (both, active and old), allowing you to rename or delete them on a per-name basis. No need of MySQL tweaking!

  • Thank you, Bettylex! To make it perfect, the plugin can be extended to appear as a button in each post’s edit page. Then, after clicking the button, the plugin will cross-check the custom fields that currently appear in the post edit page against the custom fields that are associated to the post in the database – resulting in a list of unused database custom fields that can be safely deleted.

  • @Ayre – how would you add the button to each post’s edit page? Can’t find that.

    Cheers, Richard

  • Hi RichardU, the content of my reply was a suggestion for further plugin development. The suggested button is not currently available.

  • Hey guys,

    I needed to delete a big amount of unused fields for a website we’re maintaining for years. As fields get renamed or deleted, meta fields stay unused in the database. While the size of the database gets unnecessary bigger we also had an issue in the search (since old fields were found).

    I didn’t found a proper and safe solution without a heavy SQL query to remove those data. So I created a plugin using functions provided by ACF to remove unused metadata.

    Create a database dump and give it a try:
    https://wordpress.org/plugins/whatwedo-acf-cleaner/

    Hope it helps and not just me
    Cheers Marc

  • Hey guys,

    There aren’t updates for a couple of years. And the topic isn’t super simple when conditional fields come into play.

    Nevertheless, I needed to delete a big amount of unused fields for a website we’re maintaining for years. As fields get renamed or deleted, meta fields stay unused in the database. While the size of the database gets unnecessary bigger we also had an issue in the search (since old fields were found).

    I didn’t found a proper and safe solution without a heavy SQL query to remove those data. So I created a plugin using functions provided by ACF to remove unused metadata.

    Create a database dump and give it a try:
    https://wordpress.org/plugins/whatwedo-acf-cleaner/

    Hope it helps and not just me
    Cheers Marc

  • Thanks for your work on this Marc! I’ll give it a try when I need to clean up some field data.

Viewing 25 posts - 76 through 100 (of 100 total)

The topic ‘Flush Unused Custom Fields’ is closed to new replies.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.