How-To Wordpress

How to use delete_user hook in WordPress

Managing User Deletion in WordPress with the delete_user Hook

User management is a critical aspect of any WordPress site. Ensuring that user data is handled properly during deletion is essential for maintaining data integrity and providing a seamless experience for both users and administrators. In this article, we’ll explore how to manage user deletion using the delete_user hook.

Understanding the delete_user Hook

The delete_user hook fires immediately before a user is deleted from the WordPress site. It provides developers with an opportunity to execute custom code or perform specific tasks during the user deletion process. Keep in mind that on a Multisite installation, the user is only removed from the site but not deleted from the database.



The delete_user hook accepts the following parameters:

  1. $id (int): The ID of the user to be deleted.
  2. $reassign (int|null): The ID of the user to reassign posts and links to. By default, this parameter is set to null, indicating no reassignment.
  3. $user (WP_User): The WP_User object representing the user to be deleted.

Use Cases for the delete_user Hook

Let’s explore some common scenarios where the delete_user hook can be useful:

1. Custom Table Cleanup
When your plugin or theme creates custom tables to store user-related data (such as additional profile information), you can use the delete_user hook to delete corresponding rows from those tables.

For example:

function my_custom_table_cleanup($user_id) {
// Custom table cleanup logic here
add_action('delete_user', 'my_custom_table_cleanup');

In this function, replace my_custom_table_cleanup with your actual cleanup logic. This ensures that any data associated with the user is properly removed when they are deleted.

2. Sending Notifications
Before deleting a user account, you might want to notify the user. For instance, you can send an email informing them about the impending deletion. Here’s an example of how to achieve this:

function notify_user_before_deletion($user_id) {
$user_data = get_userdata($user_id);
$email = $user_data->user_email;
$headers = 'From: ' . get_bloginfo('name') . "\r\n";
wp_mail($email, 'Account Deletion Notice', 'Your account will be deleted soon.', $headers);
add_action('delete_user', 'notify_user_before_deletion');

In this snippet, the wp_mail function sends an email to the user’s registered email address, notifying them about the upcoming account deletion. Customize the email content as needed.

Considerations – Here are a few things to keep in mind:

  1. User Meta and Fields: If you need access to user meta or fields from the user table, the delete_user hook is the right choice. You can retrieve additional information about the user using the $user parameter.
  2. Multisite Installations: For users deleted from Network Site installations (Multisite), the delete_user hook may not trigger. In such cases, consider using the wpmu_delete_user hook instead.
  3. Timing: The delete_user hook runs before the user is deleted. If you need to perform actions after deletion, explore the deleted_user hook (notice the “ed”).
  4. Data Backup and Recovery: Before deleting a user, consider creating a backup of their data. This ensures that critical information isn’t lost accidentally. You can implement a custom backup mechanism or use existing backup plugins to safeguard user-related content.
  5. User Reassignment Strategies: The $reassign parameter in the delete_user hook allows you to reassign posts and links to another user. Think strategically about how you handle reassignment:
    a. Assign to Admin: By default, WordPress assigns deleted user content to the admin (usually the site owner). While this is convenient, it might not be the best approach for large sites with multiple administrators.
    b. Custom Reassignment Logic: Consider writing custom logic to determine the most appropriate user to reassign content to. For example, you could reassign posts based on categories or tags.


By leveraging the delete_user hook effectively, you can enhance user management in your WordPress site. Whether it’s cleaning up custom tables, sending notifications, or performing other custom actions, this hook empowers you to handle user deletions with precision.
Remember to thoroughly test your custom code to ensure it behaves as expected during user deletion. Happy coding! 🚀

About the author


A coder who is trying to solve some problems via "Procoders", Software Engineer By Profession, WEB Enthusiast, Hobby Blogger. I love to share Tech Ideas and like to learn and explore new things and Technologies.

Add Comment

Click here to post a comment