It is claimed by some that Ka’b Ibn al-Ashraf was killed as a result of him annoying the Prophet (p). This is simply not true. Ka’b was a tribal leader who gave assistance to the pagan Arabs i.e., the Quraysh. The Quraish were the enemy of the Muslim community. They persecuted the Muslims for many years in Makkah, to the point that the Muslims ended up leaving, exiled to Madinah. Even when the Muslims left to get away from the Quraish tyrants, the disbelievers of Makkah hatched up many plans to exterminate the Muslim community.
Ka’b Ibn al-Ashraf gave the Quraish assistance, and organised the assassination of Prophet Muhammad (p). Hence, Prophet Muhammad (p) had decided to deal with him. The folowing Hadith shows Ashraf, was treacherous, and instigated war against Prophet Muhammad (p):
Ka’ab bin Malik who was one of those whose repentance was accepted said “Ka’ab bin Al Ashraf used to satire the Prophet and incited the infidels of the Quraish against him. …” (Sunan Abi Dawud: Book 19, Hadith 2994)
KA’B BIN AL-ASHRAF, KILLED
Ka’b bin Al-Ashraf was the most resentful Jew at Islam and the Muslims, the keenest on inflicting harm on the Messenger of Allah (p) and the most zealous advocate of waging war against him. He belonged to Tai’ tribe… 
Muslim scholar Muhammad Ali Bin Zubair Ali also comments:
After the battle of Badr, a Jew by the name of Ka’b ibnul Ashraf who harboured intense hatred for Nabee (Prophet) was greatly saddened by the defeat of the mushrikeen (idolaters) at Badr as well as the death of their leaders. As a result he travelled to Makkah to sympathise with the Mushrikeen (idolaters). While there, he composed verses expressing condolences about the dead leaders as well as poetry insulting Our Beloved Prophet (a practice he was perpetrating all along). He also incited the Mushrikeen (idolaters) to take revenge against Nabee (Prophet) and the Muslims. This was the same enemy who invited Nabee (Prophet) for a meal and has placed some people to kill him. Allah informed Nabee about this plan and he left from there before this evil plan could be executed. Many Sahabah intended killing this evil man but were exhorted by Nabee (Prophet) to exercise patience and tolerance. However when his evil crossed all limits Nabee (Prophet) permitted action against him and Muhammad bin Maslamah and other finally carried out a mission in which they annihilated this enemy of Allah from the face of the earth. 
Scholar Maulana Muhammad Ali:
KA’B IBN ASHRAF
We now come to the genuine cases which are mentioned in collections of Hadith. The first of these is the case of Ka’b ibn Ashraf. We propose to discuss it in detail, for this one case would show how the Holy Prophet has been misrepresented. Ka’b’s father belonged to the tribe of Tayy, but coming over to Madinah he became an ally of the Jewish tribe Nadir and became so influential that he succeeded in marrying the daughter of a Jewish leader. Ka’b thus stood in a very near relationship to both Jews and Arabs. When the Holy Prophet came to Madinah, the Jews made an agreement with him, by the terms of which Jews and Muslims were to live as one people, both retaining their own faith, and in the case of an attack on Madinah or an unaggressive war with a third party they bound themselves to help each other. The Prophet was accepted as the final court of appeal in all disputes.
When, however, Makkan army advanced on Madinah in the 2nd year of Hijrah, the Musims had to meet them alone, and notwithstanding that they were less than a third of the Makkan army and very inferior in efficiency and arms, they inflicted a crushing defeat on the invading army at Badr. The Muslim victory only added to the Jewish spite against Islam. Ka’b, who was bound by the Madinah treaty, now used his poetic gift freely to excite hatred of Islam and the Muslims. Not content with this, he proceeded to Makkah and openly joined hands with the enemies of Islam. He urged upon the Quraish the necessity of attacking Madinah with a strong force at an early date, and swore in the Ka’bah that he would fight against the Muslims when Madinah was invaded. Not only this; he returned from Makkah with a plan to put an end to the Prophet’s life by underhand means.
That there was a war between Muslims and non-Muslims at the time of the alleged ‘assassination,’ in the third year of the Hijrah, is an undeniable fact. The question is whether Ka’b was among the combatants or the non-combatants. If he actually joined hands with the enemies of Islam and placed himself among those who were fighting with the Muslims, and he was killed by the Muslims, can this be called a case of treachery, cruelty or butchery? That Ka’b had openly joined the combatants and become their ally is borne out by all historical accounts; nay, some of them go so far as to say that he had planned to murder the Holy Prophet treacherously. Here are a few authorities:
‘he went to the Quraish, weeping over their killed (at Badr) and inciting them to fight with the Prophet.’ (Zurqani, vol ii, p. 10)
The Prophet said): ‘He (Ka’b) has openly assumed enmity to us and speaks evil of us and he has gone over to the polytheists (who were at war with Muslims) and has made them gather against us for fighting’ (Zurqani, vol. ii, p. 11)
‘And according to Kalbi, he united in a league with the Quraish before the curtains of the Ka’bah, to fight against the Muslims.’ (Zurqani, vol. ii, p. 11)
‘And he prepared a feast, and conspired with some Jews that he would invite the prophet, and when he came they should fall on him all of a sudden.’ (Zurqani, vol. ii, p. 12)
Commenting on Bukhari’s report relating to the killing of Ka’b, the author of Fath al-Bari relates the reports which we have quoted above from Zurqani, viz., Ka’b’s going to Makkah and inciting the Quraish, entering into a league before the curtains of the Ka’bah to fight against the Muslims, the Holy Prophet’s declaration that he had assumed open enmity, and his plan to kill the Prophet by inviting him to a feast. Bukhari himself speaks of the incidents relating to the killing of ‘Ka’b under headings in which the word harb (fighting) occurs, thus showing that he was looked upon as a combatant. Abu Dawud speaks of the incident under the heading, ‘When the enemy is attacked and he unprepared.’ Showing that Ka’b was dealt with as an enemy at war with Muslims. And the comment on this is that ‘Ka’b used to incite people to murder the Muslims’, and discussing the legality of what the party sent out for the punishment of Ka’b did, the same commentator adds: ‘This is not allowed in the case of an enemy after security has been given to him or peace has been made with him … but it is allowed in the case of one who breaks the covenant and helps others in the murder of Muslims.’ And Ibn Sa’d tells us that when the Jews complained to the Holy Prophet that their leader was killed, ‘he reminded them of his deeds and how he urged and incited (the Quraish) to fight against them,’ and adds that ‘the Prophet then called upon them to make an agreement with him’, and this agreement ‘was afterwards in the possession of Ali.’ All this evidence is too clear to show that Ka’b was put to death for having broken the agreement with the Prophet and joining his enemies who were at war with him and he was therefore treated as a combatant, while the other Jews who did not go to this length, though they were not less active in speaking evil of the Holy Prophet, still lived at peace with him and all that they were required to do was to sign an agreement that they would not join hands with those who were at war with the Muslims.
The only question that is worth considering is why Ka’b was put to death by certain Muslims attacking him suddenly and unawares. In the first place, it must be clearly understood that responsibility for the manner in which he was put to death cannot lie with the Prophet. That the Prophet considered Ka’b as deserving death is quite true, but there is no proof at all that he gave any directions as to the manner in which that sentence was to be carried out. On the other hand, according to one report, when the Prophet was asked by Muhammad ibn Maslamah whether he should kill him he assumed silence, while according to another he said: ‘If you are going to do it, be not in a hurry until you have consulted Sa’d ibn Mu’adh’ (Zurqani, vol ii, p 12). At any rate he knew nothing about the details, and it is even doubtful whether the details as given are true, the Holy Prophet had nothing to do with them. And leaving aside the question of the Prophet’s responsibility, there was no other method to which resort could be had under the circumstances. The hostile critic takes it for granted that the conditions under which the Muslims lived at Madinah were very like those under which he lived in the twentieth century. They had to deal with an enemy, and they dealt with him in the only way in which it was possible to proceed under circumstances then existing. Ka’b had chosen to enter into a league with an enemy at war with Muslims, and according to all human and Divine laws he could not but be treated as an enemy at war. And dealing with him as a combatant, the Holy Prophet sent a party against him; it I definitely called a sariyyah (lit. a portion of an army) in al biographical works, this showing that the party was sent to fight with him; but it rested with the leader of the party to choose the best way in which he could deal a blow at the enemy. And Muhammad ibn Maslamah, the leader, chose a method which was recognised among the Arabs and which in his opinion was the best and most effective way under the circumstances. If the leader of the party had chosen to attack Ka’b openly, there would have been much more bloodshed, and probably the whole Jewish tribe of Bani Nadir would have suffered along with Ka’b. Ka’b had broken his agreement with the Prophet, he had revolted against him, he had entered into a league to fight against Muslims till they were extirpated, and he had secretly planned to take away the Prophet’s life. For every one of these offences he had forfeited his life. A part was sent to execute this decree and his life was taken in a manner which, if it had the fault of being secret, had also the merit of not involving innocent people along with the culprit, which would surely have been the result in the case of an open attack. But the Holy Prophet was not in any way responsible for the method of execution. 
Ibn Hajar (1372 – 1449 AD):
“The author [Bukhari] placing this in the chapter of Jihad gives the mean that Ka’b was a war enemy. … He was assassinated only because he violated his treaty and assisted in the war against the Messenger of Allah (p)…” 
Imam Al-Qastalani (1447 – 1557 AD) briefly comments:
“He violated his pact and supported the war against the Prophet (p)…” 
Another issue brought up is that Prophet Mohammad (p) approved of Maslamah to lie, and then get Ka’b killed. There is no evidence from hadith to suggest this happened. Some scholars have put words in brackets trying to explain that lying was allowed in order to kill the enemy i.e., Ka’b. However, this is not the case. We have other Hadith, and Scholars who agree that prophet Muhammad (p) did not order Maslamah to deceive Ka’b or to lie. The following evidence shows that Prophet Muhammed (p) did not encourage Maslamah to lie. Islamic scholar Allama Shibli Nu’mani comments on Ka’b ibn Ashraf’s killing. He writes:
“One his return to Medina, he began to incite people and compose satirical verses against the Prophet.2 In Arabia poetry exerted the same influence over public mind as the stirring speeches of great statesmen and the articles of leading newspapers do today. A single poet through his verse could ignite a whole tribe. There is another report to the effect that he went to Mecca with forty others, Abu Sufyan, and incited him to avenge the dead at Badr. Abu Sufyan took them all to the Haram, and there, holding the certain of the Ka’ba, they vowed vengeance for the battle of Badr. Not satisfied with all that, he made plans to get the Prophet secretly assassinated.
Allama Ya’qubi, in his history says: ‘Ka’b Ibn Ashraf, the Jew, made an effort to get the Prophet treacherously murdered.’ This report is further corroborated by another quoted by Allama Hafiz Ibn Hajar, who, while dealing with the murder of Ka’b Ibn Ashraf, states on the authority of ‘Ikrima that Ka’b invited the Prophet to feast and appointed his men to murder him when he came. Hafiz Ibn Hajar has declared this report weak, yet in view of the existing circumstances the weak points in the sources do not warrant its rejection.
Fearing more trouble from these covert designs, the Prophet spoke of it to his Companions, and with his approval, Muhammad Ibn Maslama consulted the leaders of Aus, and then as advised, murdered him in the month of Rabi al-Awwal in the 3rd year of the Hijra i.e., August 624 C.E.
Traditionists have stated that Muhammad Ibn Maslama came to the Prophet and asked for his permission in these words, ‘Let us have permission to say something.’ ‘To say something’ has been interpreted by writers on history as saying false things, which they believe, the Prophet permitted as all is fair in war. But the report in Sahih al-Bukhari has the words: ‘We may be permitted to have a talk with him.’ Sahih al-Bukhari’s words in no way suggest that permission to say something untrue was sought and given. On the other hand the dialogue reported to have taken place reveals low morals and inner mind of the Jews. Muhammad Ibn Maslama went to Ka’b and said: ‘ We gave shelter to Muhammad an incurred the wrath of the whole of Arabia. Now alms and charities are demanded of us. SO we come to you to pawn our things and get money from you.’ At this Ka’b said, ‘You will get fed up with Muhammad. All right, you should pawn your wives.’ But this beautiful face of yours, said Ibn Maslama, ‘does not let us trust the fidelity of our wives.’ Ka’b then replied, ‘then mortgage your children.’ ‘This will bring us into disgrace and disrepute throughout Arabic’, said Ibn Maslama, ‘We shall pawn our arms and you know how great is their these days.’ The report of this murder as mentioned in the Sahih al-Bukhari runs thus: ‘The people called Ka’b out of his house in a friendly manner and then pretending to smell his lock of hair caught hold of it, and killed him.’ This report does not mentioned the Prophet permitting anybody to do it. Among the Arabs of the days such methods of murder were not objectionable. 
Allama Shibli Nu’mani is quite clear that Prophet Muhammed did not permit Maslamah to lie, as there is nothing to suggest such. Those who claim that the Prophet (p) killed Ashraf because he annoyed him, are untrue. There are many Hadiths which prove that the Prophet (p) never took revenge on anyone for his own-self:
Narrated `Aisha: Allah’s Messenger never took revenge for his own self in any matter presented to him till Allah’s limits were exceeded, in which case he would take revenge for Allah’s sake. (Sahih al-Bukhari: Volume 8, Book 82, Hadith 836)
`A’ishah said: the Messenger of Allah was never given his choice between two things without taking the easier(or lesser) of them provided it involved no sin, for if it did, no one kept farther away from it than he. And the Messenger of Allah never took revenge on his own behalf for anything unless something Allah had forbidden has been transgressed, in which event he took revenge for it for Allah’s sake. (Sunan Abi Dawud: Book 42, Hadith 4767)
‘Aishah reported: Whenever the Prophet was given a choice between two matters, he would (always) choose the easier as long as it was not sinful to do so; but if it was sinful he was most strict in avoiding it. He never took revenge upon anybody for his own sake; but when Allah’s Legal Bindings were outraged, he would take revenge for Allah’s sake. (Riyad as-Salihin Book 1, Hadith 641)
Ashraf being killed was not as a result of him annoying the Prophet (p), but that he was a warmonger who wanted to assassinate the Prophet (p). The above evidences show that Prophet Muhammad (p) never took revenge on anyone who abused him verbally. Hence, Ka’b ibn al-Ashraf was killed because he was a threat to peace in the community – the Prophet (p) did not have any option left but to eliminate the danger that was posed to him, and the Muslim community. Furthermore, the assertion by some that Mohammed (p) allowed deception be used in order for Ka’b to get killed is untrue, as scholar Allama Shibli explained, there is nothing in the Hadith which suggests this.
 Ar-Raheeq Al-Makhtum – The Sealed Nectar: Biography of the Noble Prophet By Safi-Ur-Rahman al-Mubarakpuri, page 241
 Seerah Sayyidul Ambiyaa – The Noble Life of Prophet Muhammad By Muhammad Ali Bin Zubair Ali, page 139
 Muhammad the Prophet: by Maulana Muhammad Ali, page 202 – 206
 Fath al-Bari, by Ibn Hajar, page 160 and page 340
 Sharh al-Bukhari, by Al-Qastalani, volume 5, page 156
 Sirat-Un-Nabi [The Life of the Prophet] – By Allama Shibli Nu’mani, volume 2, page 92 – 94