Remove custom fields from WordPress database using SQL

I found that over time I had a vast number of custom fields set throughout my posts and pages, put there by plugins which are no longer used. In order to remove these unwanted custom fields, you need to edit each post and click on the delete option for each field no longer required, then save the post.

This is a good method if you have only one or two posts/pages that are affected, but what if you have hundreds of posts and pages. Editing each one and deleting each field is time consuming and laborious. Luckily there is a faster option to do this by directly interacting with the WordPress database and issuing SQL commands to update the relevant tables.

This quick guide shows you just that, but first please make sure you’ve taken a backup of the database, just in case.

Step 1

Login to your hosting account and launch myPHPadmin from the cPanel, or equivalent, to access the WordPress database engine.

Step 2

Enter a select SQL command to list all the meta keys you want to remove. In this example, I want to find and eventually remove the meta key dcssb_short_url.

SELECT * FROM 'wp_postmeta' where 'meta_key' = 'dcssb_short_url'

Step 3

Once you are satisfied you’ve narrowed down the selection, issue a delete SQL command to remove the rogue data

DELETE * FROM 'wp-postmeta' WHERE 'meta_key' = 'dcssb_short_url'

Step 4

Issue the same select SQL command again, to verify all the offending records have been deleted.

SELECT * FROM 'wp_postmeta' where 'meta_key' = 'dcssb_short_url'

If you get no results returned, it means that you successfully deleted the records.

1 comment

  1. Thank you so much – my SQL DB was 550MB large due to a comment plugin. I thought I’ll have to manually delete all those records, but your article saved me many hours ;)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>