In our earlier article, we have explained how to use custom fields in WordPress. In simple words, custom fields help to collect additional information from your post. You can then use the collected details for different purposes like reporting and displaying. The problem comes when there are lot of unused custom fields accumulated on your site over period. If you wonder how to delete unused custom fields in your WordPress site, here are the instructions.
Similar to custom fields, you may also have lot of unused shortcodes in your site. Check our article on how to delete unused shortcodes and avoid showing junk content to readers.
Unused Custom Fields
All meta boxes you see in post editor are examples of custom fields. By default, WordPress adds few meta boxes in the editor like “Status & visibility”, “Categories”, etc. In addition, your theme and plugins can add additional meta boxes to your site for collecting more details. Even lightweight themes like Astra and GeneratePress offer meta boxes for customizing individual post.
Here are some of the reasons why unused custom fields get accumulated in WordPress sites:
- You might have changed the theme and the new theme uses different custom fields making all the old items obsolete.
- Deleted a plugin which was using custom fields in the editor.
- You have manually created a custom filed and decided not to use after some time.
Why You Should Delete Unused Custom Fields?
WordPress stores custom fields data in wp_postmeta table. This table stores many other data like comments meta and is one of the largest tables in any WordPress installation. When you do not use custom fields, they will occupy the storage space in your database. In addition, it will slow down the database queries due to the size and impact the page loading speed of your site. Though the impact may be less in the frontend, it is always a good idea to keep things clean on the backend.
Here is one of the best examples, why you should not use bloating plugins with lot of custom fields. WP Review Pro is the popular plugin for creating a review articles in WordPress. Unlike many other review plugins, this one work differently. It uses more than 25 custom fields in the post to collect review related data. The plugin uses the collected data to showcase a review box in the post and allows you to embed the review on any other post. The problem is that these 25+ custom fields are created on each post even though you do not have any review on that post. When you switch the plugin, all those large volumes of custom fields will become useless occupying huge size in your database.
Other examples include SEO plugins like Yoast SEO which collect data using custom meta boxes.
View Custom Fields in WordPress
The first step before deleting the custom fields is to find how many are there in your site. You can find this right from the post editor without going to the backend.
- Login to your dashboard and go to “Posts > All Posts” section.
- Edit one the oldest posts in your site. Though you can also a create a new post, we recommend editing an old post. This old post could be the one you had created before changing the theme or switching the plugin.
- Click the three vertical dots on top right corner of the editor and select “Preferences”.
- Go to “Panels” tab and scroll down to “Additional” section.
- Check whether “Custom Fields” option is enabled. If not, enable it and click on “Enable & Reload” button.
- This will refresh the page you are viewing and scroll down to look below the content section on the editor.
- You can find all the custom fields used in that post under “Custom Fields” section. It is also possible to create a new field using “Add Custom Fields” button and assign a value to it.
- You can review the available fields and get an overall idea of unused fields still there on the site.
As you see in the below screenshot, all the wp_review_ fields are created by WP Review Pro plugin. We have deactivated and deleted this plugin years back. But the custom fields created by the plugin are permanently stick to the posts we had created before deleting the plugin.
Now click on the “Name” dropdown under “Add New Custom Field”. This will show the list of additional custom fields available on your site but not assigned to the post you are editing.
Delete Unused Custom Fields
With each post containing so many unused custom fields, it make sense to delete them. There are two possible ways to do that in WordPress.
- Delete from single post
- Search and delete from database
1. Delete Custom Fields from Single Post
When you view the custom fields in Gutenberg post editor, click on the “Delete” button below each value. This will delete the assigned custom filed from that post. However, you need to do this manually one by one which is an impossible task when you have hundreds of posts.
2. Search and Delete from Database
This is the correct way to delete the fields in bulk. However, first take the complete backup of your database as the changes can’t be reverted back.
- Login to your hosting account and go to cPanel or the custom panel (like Site Tools in SiteGround).
- Search and open phpMyAdmin app which is used for managing your database.
- When you are in phpMyAdmin, make sure to select the correct database and click “wp_postmeta” table from left sidebar. Note that the default “wp_” is the table prefix used in WordPress, you may have different prefix for tables in your database.
- Under the “meta_key” column, you should see the custom field names that you see in Gutenberg post editor.
- Simply click “Delete” and confirm to remove the entry from your database.
- You can either delete one by one or select multiple values once and delete them in bulk.
Though you can bulk delete the custom fields, it is difficult to find meta_key for all the posts as each post will have a set of custom fields. Therefore, go to “Search” tab from the top navigation and select “Table Search” button. Paste your custom field value in the meta_key “Value” field. Make sure to select the operator as “=” and click on “Go” button.
This will list all the occurrences of the given custom value in your database. Scroll to the bottom and change the number of rows to 500 which is the maximum value. Now, click “Check all” to select all the items and click “Delete” link as shown in the below picture. Confirm the deletion and all the unused custom fields will be deleted from your site. You can repeat this for each value and clean up the database.
Alternatively, you can go to “SQL” tab and paste the below query in the box. Hit “Go” button to get the same search results as above and delete the entries in bulk.
SELECT * FROM `wp_postmeta` WHERE `meta_key` = 'custom_field_name'
Final Words
WordPress will not delete the unused custom fields to help using the plugin at any point later. For example, you decided to delete a review plugin and after few months changed the mind and want to reuse again. In this case, all your previously created reviews will be available as the custom field values are permanently stored in database. However, over period you can find thousands of unused entries in postmeta table and finding the unused fields will become a huge task for cleaning up. We also do not see any theme or plugin author has clear documentation on how many custom fields will be added in database when using their product. We hope this article helped you in understanding the basics and deleting unused custom fields in WordPress site.
2 Comments
Leave your reply.