BP_Messages_Message::delete_user_message( int $user_id )

Delete all the message send by user




(int) (Required) user id whom message should get deleted


File: bp-messages/classes/class-bp-messages-message.php

	public static function delete_user_message( $user_id ){
		global $wpdb;

		$bp = buddypress();

		// Get the message ids in order to delete their metas.
		$message_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT (id) FROM {$bp->messages->table_name_messages} WHERE sender_id = %d", $user_id ) );
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE sender_id = %d", $user_id ) );

		// Delete message meta.
		foreach ( $message_ids as $message_id ) {
			bp_messages_delete_meta( $message_id );

		// delete all the meta recipients from user table.
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE user_id = %d", $user_id ) );


We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.