Skip to main content

Batch delete custom fields from your WordPress pages and posts

TechGasp Delete Custom Fields

In new WordPress installations it’s normal to test a bunch of plugins during the initial website setup also, if you run your WordPress website for a few years you probably have installed and tested a ton of plugins that you are not using anymore and have since then deleted or deactivated. That’s all fine, the issue is that most of the plugins do not delete their posts and pages meta_keys.

The Problem

All the posts and pages meta keys add weight to your wordpress database, it just gets bigger and slower. But that’s not all, each time a page or posts is loaded on your wordpress frontend, the post or page tries to load from the database all the meta keys for that specific post or page making your website slower for the user experience. Your Google page rank also gets affected since your website takes more time to load, slower page load times.

Each post or page meta keys can be seen using the Custom Fields meta box inside your wordpress editor. If the meta box is not being displayed below your post or page text editor, then activate it inside screen options. See screenshot below.

TechGasp Custom Fields

Once you have done that you can visualize the Custom Fields metabox with all meta keys for that posts or page.

TechGasp Delete Custom Fields

You are probably thinking now “it’s easy to delete”, just press the delete button in each field. Well true, but multiply that by hundreds of fields and then add all your posts and pages because you will have to repeat the process in each page or posts… may take weeks!

All the hassle can be easily fixed and it only takes a couple of minutes. Let’s see how to get your database trimmed and slim with pristine page load times.

How to Clean your WordPress Database

Now the good stuff, we are going to achieve this using a couple of sql queries inside the wordpress database.
You can reach your database using your hosting control panel software to manage databases, 99% of the times the software used by the hosting companies is phpMyAdmin.

Step 1

Once in your hosting panel phpMyAdmin, if you have more than one database, select the correct wordpress database on the left side menu.

Step 2

Time to backup the database before running any sql queries. Select Export -> Export Method: Quick -> Format: SQL -> GO
This will download your full wordpress database to your computer. It might take a few minutes to complete depending on your database size and hosting overall processing speed.

Step 3 Delete 1 Meta Key

We are now going to clean stuff. In phpMyAdmin select the correct database on the left side menu, and click the plus button to expand the database tables.
Select the wp_postmeta table and at the top of the page select SQL.
You will see a predefined command SELECT * FROM `wp_postmeta` WHERE 1, delete it and paste:

DELETE FROM wp_postmeta WHERE meta_key = 'REPLACE_WITH_YOUR_META_KEY'

The REPLACE_WITH_YOUR_META_KEY should be the actual key you want to delete. You can check them using the Custom Fields meta box. Taking the above screenshot as example the meta key I would use is deals_code because I know this is an old entry not being used by any plugin. My code would look like:

DELETE FROM wp_postmeta WHERE meta_key = 'deals_code'

After Inserting the correct SQL statement just press GO to delete all entries of that meta key in all your posts and pages.

Step 3 Batch Delete Meta Keys

The above query deletes just 1 meta key. If you have hundreds or thousands it would take a long time to finish. So, let’s change the query in order to delete more that one meta key across all of your pages and posts.

DELETE FROM wp_postmeta WHERE `meta_key` IN ('REPLACE_WITH_YOUR_META_KEY', 'REPLACE_WITH_YOUR_META_KEY')

Once again the REPLACE_WITH_YOUR_META_KEY should be the actual key you want to delete. Check them using Custom Fields. Taking the above Custom Fields screenshot as example the meta key I would use is deals_code and master_h. My code would look like:

DELETE FROM wp_postmeta WHERE `meta_key` IN ('deals_code', 'master_h')

You can add more meta keys to your query to batch delete more, just add , 'YOUR_META_KEY' inside the parenthesis. Example:

DELETE FROM wp_postmeta WHERE `meta_key` IN ('REPLACE_WITH_YOUR_META_KEY', 'REPLACE_WITH_YOUR_META_KEY', 'REPLACE_WITH_YOUR_META_KEY', 'REPLACE_WITH_YOUR_META_KEY')

That’s it, once you are done you will have a smaller, trimmed database and faster page load times. Always make sure to backup your database before running any query and that the meta key is not being used by any plugin.

No Comments yet!