Recently one of our project we need to delete some date from the custom table once user deleted. So that 10 minutes of google got the solutions and solutions is as per below.
Note: Once the user deletes that time “delete_user” action will call and action is as per below.
add_action( 'delete_user', 'function_name' );
Let’s I try to explain step by step how to delete data in the custom table.
In our case, we have to delete data in six table and all tables are as per screenshot.
PHP Code is as per below.
function zi_delete_user( $user_id )
// get user details base on user id
$user_obj = get_userdata( $user_id );
// Array of all tables
$tables = array('wp_zi_answer', 'wp_zi_answer_interventions_basic', 'wp_zi_daily_answer', 'wp_zi_goal_basic', 'wp_zi_lifestyle_answer', 'wp_zi_setting');
foreach($tables as $table)
// Delete all data have deleted user id
$query_zi_table = $wpdb->query("DELETE FROM `user_id` where `user_id` = ".$user_obj->ID."");
add_action( 'delete_user', 'zi_delete_user' );
Check and let me know have any question.