WordPress Optimization – Removing Metadata from Deleted Posts

On this page
  1. Access phpMyAdmin
  2. Log In to the Database
  3. Open the SQL Query Editor
  4. Remove Orphaned Metadata
  5. What Does This Query Do?
  6. Benefits of Removing Orphaned Metadata
  7. Check Before Deleting (Optional)
  8. Alternative Cleanup Methods
  9. Summary

WordPress stores additional information related to posts, pages, and custom content types in a separate database table called wp_postmeta.

This table contains metadata used by:

  • WordPress core features
  • Themes
  • Plugins
  • Custom fields
  • E-commerce extensions
  • Page builders

When posts or pages are deleted, associated metadata may sometimes remain in the database. Over time, these orphaned records can accumulate and unnecessarily increase the size of the database.

This guide explains how to identify and remove metadata entries that no longer belong to existing posts.

Important

This guide is intended for users with basic database administration knowledge.

Before executing any SQL queries:

  • Create a complete database backup.
  • Verify that you have selected the correct database.
  • Test your website after performing any cleanup operations.

Removing data incorrectly may result in website functionality issues.

Access phpMyAdmin

  1. Log in to the mybox Panel.
  2. Navigate to:
Databases
  1. Click:
phpMyAdmin

A new browser tab will open with the database login screen.

Log In to the Database

Use the database credentials associated with your WordPress website.

If you do not know the database credentials, you can find them in the wp-config.php file located in the root directory of your WordPress installation.

Example:

define('DB_NAME', 'database_name');
define('DB_USER', 'database_user');
define('DB_PASSWORD', 'database_password');
define('DB_HOST', 'database_host');

The file is typically located in:

public_html

Open the SQL Query Editor

After logging in:

  1. Select your WordPress database from the left-hand menu.
  2. Click the:
SQL

tab at the top of the page.

This will open the query editor.

Remove Orphaned Metadata

Enter the following SQL query:

DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL;

Click:

Execute

to run the query.

What Does This Query Do?

The query searches for entries in the wp_postmeta table whose associated post no longer exists in the wp_posts table.

These records are considered orphaned metadata.

The query removes metadata linked to:

  • Deleted posts
  • Deleted pages
  • Deleted custom post types
  • Content removed by plugins
  • Other non-existent WordPress content

Benefits of Removing Orphaned Metadata

Cleaning unused metadata can provide several benefits:

  • Reduced database size.
  • Faster database backups.
  • Improved query performance.
  • Reduced storage consumption.
  • Better overall WordPress performance.

The impact is especially noticeable on websites that:

  • Have been running for a long time.
  • Frequently delete or update content.
  • Use many plugins.
  • Have imported or migrated content multiple times.

Check Before Deleting (Optional)

If you want to review the records before deleting them, run:

SELECT pm.*
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL;

This query displays the orphaned metadata without removing it.

After reviewing the results, you can decide whether to proceed with the cleanup.

Alternative Cleanup Methods

If you prefer not to execute SQL commands manually, several WordPress optimization plugins can perform this cleanup automatically, including:

  • WP-Optimize
  • Advanced Database Cleaner
  • LiteSpeed Cache
  • WP Rocket

These plugins can identify and remove orphaned metadata directly from the WordPress dashboard.

Summary

Over time, WordPress may accumulate metadata entries associated with posts that no longer exist. These orphaned records occupy space and can contribute to database bloat.

To remove them:

  1. Open phpMyAdmin.
  2. Select your WordPress database.
  3. Open the SQL tab.
  4. Execute:
DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL;

Always create a database backup before performing cleanup operations and verify that your website functions correctly afterward.

Didn't find an answer?

Our support team is here for you 24/7 – write to us and we will help you sort it out.