Database category deletion issues
August 7, 2025 ยท AI Conversations
—
### โ
What Actually Happens:
When you delete product categories via **wp-admin**:
– WordPress removes the **term** from `wp_terms`.
– But related entries in other tables like `wp_term_relationships`, `wp_term_taxonomy`, and even some post meta entries **might remain**, especially if:
– Deletion failed silently.
– The category had a parent-child relation or was used in custom code.
– A plugin prevents full deletion for "soft-delete" behavior.
—
### ๐ Check Where They Might Still Exist:
Check the following database tables for remnants:
| Table | Purpose |
|——-|———|
| `wp_terms` | The category name (should be gone) |
| `wp_term_taxonomy` | Contains taxonomy type like `product_cat` |
| `wp_term_relationships` | Links products/posts to categories |
| `wp_termmeta` | Metadata for terms (may still exist) |
—
### ๐งผ Safe Cleanup Process
> **โ ๏ธ Backup your database before running any manual SQL commands.**
#### 1. Check for orphaned taxonomies:
SELECT * FROM wp_term_taxonomy
WHERE taxonomy = 'product_cat'
AND term_id NOT IN (SELECT term_id FROM wp_terms);
#### 2. Delete orphaned taxonomies:
DELETE FROM wp_term_taxonomy
WHERE taxonomy = 'product_cat'
AND term_id NOT IN (SELECT term_id FROM wp_terms);
#### 3. Delete unused relationships:
DELETE FROM wp_term_relationships
WHERE term_taxonomy_id NOT IN (SELECT term_taxonomy_id FROM wp_term_taxonomy);
#### 4. Delete orphaned term meta:
DELETE FROM wp_termmeta
WHERE term_id NOT IN (SELECT term_id FROM wp_terms);
—
### โ After Cleanup
– Clear object cache if any caching plugin is active.
– Re-check in **WooCommerce > Products > Categories**.
– If using WPML or Polylang, some ghost entries might still be there in their custom tables too.
—
Let me know if you'd like me to write a PHP script or WP-CLI command to do all this safely.