President Buhari said he was deeply shocked by the news in a statement released on Saturday.

The president – who faces an election in three months time – added no responsible leader “would rest on his oars… to allow terrorists to endanger the lives of its military personnel and other citizens”.

As a result, he planned to meet with military and intelligence chiefs “in the coming days” to plan their “next steps”.

Mr Buhari came to power in 2015 after promising to defeat Boko Haram militants, who have caused havoc in Nigeria through a wave of attacks as they attempt to overthrow the government and create an Islamic state.

But while the army has retaken most of the territory the militants once controlled, they are still able to carry out deadly attacks.

In a statement, released on Friday evening, the army admits that it is operating in “trying times”, which is a rare if veiled admission that the military is experiencing serious setbacks in the fight against the jihadists, says BBC Africa editor Will Ross.

On Monday, a Boko Haram faction loyal to the Islamic State group, known as the Islamic State West Africa Province, said it had carried out the attack and put the death toll at at least 40.

President Buhari’s political opponents in the Senate said 44 soldiers had died.