Skip to content

Instantly share code, notes, and snippets.

@rana01645
Created March 23, 2025 10:11
Show Gist options
  • Save rana01645/aa37457f067582d945340918db04b216 to your computer and use it in GitHub Desktop.
Save rana01645/aa37457f067582d945340918db04b216 to your computer and use it in GitHub Desktop.
Cleanup wordpress spam user
-- ========================================
-- πŸ”„ WordPress Scam Cleanup Script
-- πŸ§‘ Clean all content, meta, tags, attachments, and user data for a specific user
-- πŸ’‘ Replace the value of @user_id to clean up another account
-- ========================================
-- Step 0: Set the user ID to clean up
SET @user_id := 110133;
-- Step 1: Create a temporary table for all pending posts by this user
CREATE TEMPORARY TABLE temp_pending_posts (
ID BIGINT UNSIGNED PRIMARY KEY
);
INSERT INTO temp_pending_posts (ID)
SELECT ID
FROM trickbd_posts
WHERE post_author = @user_id
AND post_status = 'pending'
AND post_type = 'post';
-- Step 2: Delete post meta for pending posts
DELETE FROM trickbd_postmeta
WHERE post_id IN (SELECT ID FROM temp_pending_posts);
-- Step 3: Remove term relationships (tags/categories) from those posts
DELETE FROM trickbd_term_relationships
WHERE object_id IN (SELECT ID FROM temp_pending_posts);
-- Step 4: Delete meta of attachments linked to the pending posts
DELETE FROM trickbd_postmeta
WHERE post_id IN (
SELECT ID FROM trickbd_posts
WHERE post_type = 'attachment'
AND post_parent IN (SELECT ID FROM temp_pending_posts)
);
-- Step 5: Delete attachment posts themselves
DELETE FROM trickbd_posts
WHERE post_type = 'attachment'
AND post_parent IN (SELECT ID FROM temp_pending_posts);
-- Step 6: Delete the pending posts
DELETE FROM trickbd_posts
WHERE ID IN (SELECT ID FROM temp_pending_posts);
-- Step 7: Clean up unused tags (post_tag taxonomy only)
DELETE FROM trickbd_term_taxonomy
WHERE taxonomy = 'post_tag'
AND term_taxonomy_id NOT IN (
SELECT DISTINCT term_taxonomy_id FROM trickbd_term_relationships
);
-- Step 8: Clean up unused terms (orphaned from taxonomy)
DELETE FROM trickbd_terms
WHERE term_id NOT IN (
SELECT DISTINCT term_id FROM trickbd_term_taxonomy
);
-- Step 9: Remove comments by the user (if any)
DELETE FROM trickbd_comments
WHERE user_id = @user_id;
-- Step 10: Clean up orphaned comment meta
DELETE FROM trickbd_commentmeta
WHERE comment_id NOT IN (
SELECT comment_ID FROM trickbd_comments
);
-- Step 11: Delete user's other non-published posts (drafts, auto-drafts, etc.)
DELETE FROM trickbd_posts
WHERE post_author = @user_id
AND post_status IN ('draft', 'future', 'private', 'auto-draft');
-- Step 12: Remove any other term relationships from this user's posts
DELETE FROM trickbd_term_relationships
WHERE object_id IN (
SELECT ID FROM trickbd_posts
WHERE post_author = @user_id
);
-- Step 13: Delete user meta and user account
DELETE FROM trickbd_usermeta WHERE user_id = @user_id;
DELETE FROM trickbd_users WHERE ID = @user_id;
-- Step 14: Drop the temporary table used for pending post tracking
DROP TEMPORARY TABLE IF EXISTS temp_pending_posts;
-- βœ… Cleanup complete for user ID = @user_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment