When doing WordPress development, we often spend a lot of time debugging the database. If we delete a post directly from the database, the custom field data that belongs to that post is not deleted along with it. That leaves behind orphaned data because those rows are no longer associated with any post.
These orphaned post meta rows no longer have any chance of being accessed. If too much of that data accumulates, it adds unnecessary overhead to the database. Cleaning it up is a practical way to improve WordPress database performance.
We can remove this data in batches with a SQL statement. Run the following command directly in phpMyAdmin or another MySQL management tool to clean out those useless rows.
DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL
Method 2: Clean it up with the Easy WP Cleaner database plugin
Not every WordPress site is managed through direct SQL access, and not every user is comfortable working with SQL commands. Besides using SQL directly, we can also rely on a database-cleaning plugin to remove this useless data. A plugin we commonly use for this purpose is Easy WP Cleaner.
The plugin is very easy to use. After installing it, you simply click the delete button on the plugin page to run the cleanup. In addition to cleaning useless Post Meta data, it can also remove other kinds of orphaned data, such as useless comments, drafts, revisions, and more. It is a very convenient option and one we strongly recommend.
If the database contains a large amount of orphaned data like this, you will often notice that the database becomes smaller after cleanup, and some queries may run faster as well.
