Black Friday - Cyber Monday Sale
30% OFF All Premium Licenses
  • 00 Days
  • 00 Hrs
  • 00 Mins
  • 00 Secs
Use code
SIGMA30

This 30% discount applies only to the first year. From the second year, renewals are charged at the regular price.

What are unused relationships and how to clean them?

Younes JFR.

What are unused relationships?

WordPress stores the connections between posts and taxonomy terms (like categories or tags) in the wp_term_relationships table. Sometimes, when posts or taxonomies are deleted, their relationships remain in this table even though they no longer point to any valid post or term. These are called unused relationships.

How they affect your database?

Unused relationships clutter the wp_term_relationships table, creating unnecessary entries that increase database size and slightly slow down taxonomy queries. They can also cause inaccurate term counts and add extra load during backups and exports.

How to clean them?

1. Using Advanced Database Cleaner plugin

You can easily remove unused relationships using the General Cleanup module in Advanced Database Cleaner plugin.

The plugin automatically detects unused relationships and allows you to:

  • Safely delete all unused relationships.
  • Schedule automatic cleanups to prevent unnecessary buildup.

2. Using a raw SQL query

If you prefer a manual cleanup, you can run the following SQL command in phpMyAdmin for example:

DELETE FROM term_relationships
WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM posts);

In multisite installations, execute this query for each subsite, replacing wp_ with the subsite’s prefix (for example, wp_2_, wp_3_, etc.).

Note:
While you can manually run these SQL commands to clean unused relationships, we strongly recommend using the Advanced Database Cleaner plugin.
It uses WordPress’s internal cleaning methods, automatically handles multisite installations, and ensures that all related data is safely removed while keeping your database consistent and stable.