If you have an older WordPress blog, you will notice that there are a lot of custom fields meta_keys in the WordPress custom fields options.
The problem is that you’ll see a lot of unused custom field values that were added by some old theme or plugin you used long ago.
Here's a screenshot of a ShoutMeLoud custom field with a lot of custom values (many of which are due to old themes or plugins).
Now, if you use custom fields to add values to your blog posts, you might find these useless meta_keys annoying.
In this article, I will share how to delete WordPress custom fields from your database.
- IMPORTANT: Perform this procedure at your own risk. Make sure to have a full backup of your WordPress database.
Unused custom fields
All of the meta boxes you see in the post editor are examples of custom fields. By default, WordPress adds a few meta boxes to the editor, such as Status and Visibility, Category, and more. Additionally, your theme and plugins can add additional meta boxes to your site to collect more details. Even lightweight themes like Astra and GeneratePress offer meta boxes for customizing individual posts.
Here are some reasons why unused custom fields accumulate in your WordPress site:
- You may have changed themes and the new theme uses different custom fields, making all old items obsolete.
- Removed plugins that used custom fields in the editor.
- You have manually created a custom file and decide not to use it after a while.
Why should you delete unused custom fields?
WordPress stores custom fields data in the wp_postmeta table. This table stores a lot of other data, such as comment metadata, and is one of the largest tables in any WordPress installation. If you don’t use custom fields, they will take up storage space in your database. Additionally, due to their size, custom fields can slow down database queries and affect your site’s page load speed. While the impact on the front-end may be minor, it’s always a good idea to keep your back-end tidy.
This is a prime example of why you should not use bloated plugins with tons of custom fields. WP Review Pro is a popular plugin for creating review posts in WordPress. Unlike many other review plugins, this one works differently. It uses 25+ custom fields in the post to collect review related data. The plugin uses the collected data to display the comment box in the post and allows you to embed the review in any other post. The problem is, these 25+ custom fields are created on every post even if you don’t have any comments on that post. When you switch plugins, all these tons of custom fields will become useless and take up huge space in your database.
Other examples include SEO plugins , such as Yoast SEO , which collect data using custom meta boxes.
Delete unused custom fields
Since each post contains a lot of unused custom fields, it is very necessary to delete them. There are several ways to delete them in WordPress, depending on whether you want to delete them or not:
- Single or multiple posts.
- A custom field or bulk field.
The first step before deleting custom fields is to confirm how many custom fields you have on your website. Therefore, all the options described in this article will guide you on how to review the fields before deleting them.
1. Remove custom fields from a single post in the Gutenberg editor
You can view and delete custom fields right from the post editor without going to the backend.
- Log in to your dashboard and go to the Posts > All Posts section.
- Edit the oldest post on your site. Although you can also create a new post, we recommend editing an old post. This old post should be one you created before you changed themes or switched plugins.
- Click the three vertical dots (Options) in the upper right corner of the editor and select the Preferences menu.
- Go to the General tab and scroll down to the Advanced section.
- Check if the Custom Fields option is enabled. If not, enable it and click on the Show and Reload Page button.
Displaying custom fields in the Gutenberg editor
- This will refresh the page you are viewing and scroll down to view the content section on the editor.
- You can find all the custom fields used in this post under the Custom Fields section.
- While viewing custom fields in the Gutenberg post editor, click the “Remove” button below each value. This will remove the assigned custom field from that post.
You can also use the Add Custom Field button to create new fields and assign values to them. Now, click on the Name drop-down menu under Add New Custom Field. This will display a list of other custom fields available on your site that have not yet been assigned to the article you are editing. You can review the available fields and get an overview of the fields that are still unused on your site.
View custom fields from the drop-down menu
As you can see in the screenshot below, all of the wp_review_ fields were created by the WP Review Pro plugin. We deactivated and deleted this plugin several years ago. However, the custom fields created by the plugin remain permanently in posts created before we deleted the plugin. Therefore, we can delete these fields to clean up individual posts.
Custom fields from comments plugin
2. Delete custom fields in the classic editor
If you’re using the old classic editor or a custom post type like WooCommerce Products, follow these steps.
- Edit one of your old posts.
- In the classic editor interface, click the "Screen Options" button in the upper right corner of the screen to expand it.
- Check the “Custom Fields” box and click the “Screen Options” button again to close that section.
Display custom fields in the classic editor
- Scroll down to the bottom of the content and find the Custom Fields meta box.
- Click the Delete button below each field to permanently delete it from the database.
Deleting custom fields in the classic editor
Please note that deleting custom fields is an irreversible action and will update immediately without publishing or saving your content. Therefore, make sure to only delete unused fields, not required fields. While some fields may be automatically generated, many will not be populated again. For example, deleting WooCommerce-related fields will affect your reporting.
3. Delete custom fields for a single post from the database
While the above options are easy to handle, you will need to delete the fields one by one in the editor. Another way is to delete from the database, which will allow you to delete one or more items in your post. However, please make a complete backup of your database first, as the changes cannot be recovered.
- Log in to your hosting account and go to cPanel or a custom panel (like Site Tools in SiteGround).
- Search for and open the phpMyAdmin application for managing your database.
- Once in phpMyAdmin, make sure the correct database is selected and click on the "wp_postmeta" table in the left sidebar. Note that the default "wp_" is the table prefix used by WordPress and may be different in your database.
- For a single post (post_id column), you should see all the custom field names you saw under the "meta_key" column in the Gutenberg/Classic post editor.
- Simply click Delete and confirm to remove the entry from the database.
- You can delete them one by one, or select multiple values at once and delete them in bulk for a single post.
Viewing and deleting custom fields in phpMyAdmin
4. Use SQL table search to delete in batches
When you have hundreds of posts, it is almost impossible to manually delete fields one by one or post by post. Therefore, the ideal approach is to delete fields from multiple posts in bulk. However, you need to customize the field name (meta_key in the database) to achieve this. So, first prepare a list of fields to be deleted from the post editor or database and follow the steps below.
- Open the phpMyAdmin application in your hosting panel.
- Select your database, then select the 'wp-postmeta' table (under the 'Structure' tab).
- Go to the Search tab from the top navigation and select the Table Search button.
- Paste your custom field value into the meta_key "Value" field.
- Make sure the operator "=" is selected and click the "Go" button.
Search for custom fields in the database
- This will show all matches of the specified custom field name in the database. Scroll to the bottom and if the "Show All" option is displayed, check it. If the list has more than 500 rows, you may not see this option. You will see a prompt asking you to confirm whether to show all rows as this may crash your browser, then click the "OK" button. Otherwise, just change the number of rows to the maximum value of 500.
Confirm that all rows are displayed
- Now, click on Check All to select all the items and then click on the Delete link.
Batch delete custom fields in the database
- You will be presented with a list of 500 rows of data to be deleted. Click the "Go" button to confirm the deletion and all unused custom fields will be removed from your website.
Confirm to delete the batch fields
You can repeat this for every 500 groups and each custom file name to clean up the database .
5. Use SQL query to delete in batches
You don't need to know SQL to use a query to bulk select custom fields. When you use the "Table Search" as described above, the results section will show the SQL query at the top, then click the "Edit" link.
SQL query for table search results
It will take you to the SQL tab with the query inserted. Simply change the custom field name and hit the Execute button to get the same search results as above with the entries deleted in bulk.
Use SQL query to batch delete a single custom field
Following is the query format, you can use it by replacing the correct table prefix and custom field name.
01.SELECT * FROM `wp_postmeta` WHERE `meta_key` = 'custom_field_name'
It is also possible to use multiple queries to view different custom fields as shown below. However, the problem is that the results will be displayed grouped by each field name, which limits you to selecting rows for a single field and deleting the entries. Therefore, it is not possible to select all rows for multiple custom fields and delete them in bulk.
SELECT * FROM `wp_postmeta` WHERE `meta_key` = 'custom_field_name_1';
SELECT * FROM `wp_postmeta` WHERE `meta_key` = 'custom_field_name_2';
SELECT * FROM `wp_postmeta` WHERE `meta_key` = 'custom_field_name_3'
Final Words
This way, you can get rid of all unwanted WordPress custom field values and keep your blog database optimized .
I recommend that you only perform this task if you are confident enough. It's not rocket science, but one wrong click can get you into a lot of trouble.
I also recommend that you try an advanced database cleaning plugin after this to further clean up your WordPress database.
I hope this tutorial was easy to follow. If you have any questions, feel free to leave them in the comments. If you liked this article, don’t forget to share it on Facebook, Twitter, and Google+!
Disclosure: Some of the links in this article contain affiliate links, which means we may earn a commission if you click through to visit us, at no extra cost to you. See how SidelinePlay is funded, why it’s important, and how you can support us.
Was this helpful?