When you delete a user, keep their comments for the conversation but remove their name, email, website and IP address.
add_action( 'delete_user', function ( int $user_id ): void {
$comments = get_comments( [
'user_id' => $user_id,
'status' => 'all',
'fields' => 'ids',
'number' => 0,
] );
foreach ( $comments as $comment_id ) {
$comment = get_comment( $comment_id );
wp_update_comment( [
'comment_ID' => $comment_id,
'comment_author' => 'Former member',
'comment_author_email' => '',
'comment_author_url' => '',
'comment_author_IP' => wp_privacy_anonymize_ip( (string) $comment->comment_author_IP ),
'comment_agent' => '',
'user_id' => 0,
] );
}
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
When a user account is deleted, WordPress keeps their comments, still showing their name, email and IP address. If someone asks you to delete their account, they likely expect that information to go too. This snippet anonymises their comments while keeping the discussion readable.
The delete_user action runs just before a user is deleted. The snippet finds all their comments, in any status, and updates each one with wp_update_comment(): the name becomes “Former member”, email, website and user agent are cleared, the IP address is shortened and the link to the user account is removed.
The comment text stays. If the comment itself contains personal details, edit or delete it separately.
Create a test user, post a comment as them, then delete the user. The comment should show “Former member” with no email.