MySQL stored procedures with WordPress tables: a cleanup routine
A stored procedure is a named block of SQL statements saved in the database and run with CALL. For maintenance that is always the same sequence of queries, such as removing orphaned meta and relationships after a careless import, a procedure turns a page of SQL into one command. Like views, they are a tool for sites you administer, not for code that ships in a plugin.
Problem: After imports and deletions done with SQL, the database collects post meta, comment meta and term relationships of posts that no longer exist, and the cleanup queries are run by hand every few months.
Solution: Create a procedure with CREATE PROCEDURE (changing the delimiter so the semicolons inside do not end the statement), put the cleanup queries into it, and run it with CALL after a backup. Drop it when it is no longer needed.
The examples use the wp_ prefix; replace it with yours.
The procedure
In the phpMyAdmin SQL tab or the mysql client:
DELIMITER $$
CREATE PROCEDURE ha_cleanup_orphans()
BEGIN
DELETE pm FROM wp_postmeta pm
WHERE NOT EXISTS ( SELECT 1 FROM wp_posts p WHERE p.ID = pm.post_id );
DELETE tr FROM wp_term_relationships tr
WHERE NOT EXISTS ( SELECT 1 FROM wp_posts p WHERE p.ID = tr.object_id )
AND NOT EXISTS ( SELECT 1 FROM wp_links l WHERE l.link_id = tr.object_id );
DELETE cm FROM wp_commentmeta cm
WHERE NOT EXISTS ( SELECT 1 FROM wp_comments c WHERE c.comment_ID = cm.comment_id );
SELECT ROW_COUNT() AS last_statement_rows;
END$$
DELIMITER ;
DELIMITER $$ tells the client that statements end with $$ for now, so the semicolons inside the procedure body are stored as part of it. phpMyAdmin has a “Delimiter” field under the SQL box that does the same.
Term relationships can also belong to links (the old link manager), which is why the second query checks wp_links too.
Running it
CALL ha_cleanup_orphans();
mysqldump -u user -p helloadmin > before-cleanup.sql mysql -u user -p helloadmin -e "CALL ha_cleanup_orphans();"
Always make the dump first. The procedure deletes rows; there is no undo.
Parameters
DELIMITER $$
CREATE PROCEDURE ha_delete_old_revisions( IN keep_days INT )
BEGIN
DELETE p FROM wp_posts p
WHERE p.post_type = 'revision'
AND p.post_modified < NOW() - INTERVAL keep_days DAY;
END$$
DELIMITER ;
CALL ha_delete_old_revisions( 90 );
Deleting revisions this way leaves their meta rows behind, which the first procedure removes afterwards. Deleting through WordPress (wp post delete) avoids that and runs the hooks caching plugins expect; use SQL for large volumes where WP-CLI is too slow.
The catches
- Creating procedures needs the
CREATE ROUTINEprivilege, often missing on shared hosting. - A procedure is stored with a
DEFINER. After moving the database to another server, calling it can fail if that user does not exist there. - Many backup and migration tools skip procedures (
mysqldumpneeds--routines), so they quietly disappear after a restore. - The table names are fixed in the body; a different prefix needs a different procedure.
For a plugin that runs on unknown hosts, the same cleanup written in PHP with $wpdb and run from WP-CLI or a cron job is more portable.
NOTE: Remove procedures you no longer use (DROP PROCEDURE IF EXISTS ha_cleanup_orphans;). A forgotten routine that deletes rows is a risk: anybody with database access can run it, and nobody remembers what it does.