The Battle of the Trench (), also known as the Battle of Khandaq () and the Battle of the Confederates (), was part of the conflict between the Muslims and the Quraysh. The Quraysh advanced towards the Muslims, who defended themselves in Medina by digging a trench around their settlement at the suggestion of Salman the Persian. The battle took place in 627 and lasted around two weeks, resulting in five to six casualties reported by the Muslim, and three casualties amongst the Quraysh.

The Quraysh launched an attack, but realized soon after that they had very limited military capability, being primarily merchants. This prompted negotiations with the Bedouins in order to get them to join the campaign. The Banu Nadir, whom Muhammad had previously expelled from Medina on account of an attempted assassination (crime against the state), were also part of this effort and offered the Bedouins half of their crops in Khaybar to persuade them to participate in attacking the Muslims. They reported to have gathered a confederate force of between 7,500 and 10,000 men, including Banu Ghatafan, Banu Sulaym, and Banu Asad.

Muhammad, having learned of the impending hostile advance from the Quraysh, took the advice of Salman the Persian to have his followers make a deep trench to impede the attackers' movement. When the Quraysh advanced, their unfamiliarity with this tactic caused them to struggle getting beyond the trench. Muhammad used this time to negotiate secretly with the Banu Ghatafan. After about two weeks, the weather deteriorated and the invading party withdrew.

Consequently, the Muslims retaliated against the Quraysh for this attack, and upon the latter's unconditional surrender, armed combatants were killed, with unarmed men, the elderly, women and children initially captured as prisoners of war and released upon the Islamic condition of Fidyah. Those unable to pay the Fidyah were released upon the condition of acting as literary teachers for 10 children.

The failed offensive caused the Meccans to lose their trade to Syria and much of their prestige.

Name The battle is named after the "Trench", or khandaq, that was dug by Muslims in preparation for the battle as an act of defense. The word khandaq () is the Arabised form of the Middle Persian word kandag (; meaning "that which has been dug"). Salman the Persian advised Muhammad to dig a trench around the city. The battle is also referred to as the Battle of Confederates (). The Qur'an uses the term confederates () in to denote the confederacy of non-believers and Jews against Islam.

Background After their expulsion from Mecca, the Muslims fought the Meccan Quraysh at the Battle of Badr in 624, and at the Battle of Uhud in 625. Although the Muslims were defeated at the Battle of Uhud, their strength was gradually growing. In April 626 Muhammad raised a force of 300 men and 10 horses to meet the Quraysh army of 1,000 at Badr, where the latter had threatened to attack. Although no fighting occurred, the coastal tribes were impressed with Muslim power. Muhammad also tried, with limited success, to break up many confederacies against Muslims. Nevertheless, he was unable to prevent the Meccan one.

The Confederates

Early in 627, leaders of the exiled Banu Nadir, including Huyayy ibn Akhtab, traveled from Khaybar to Mecca to forge a military alliance with the Quraysh aimed at eliminating the Muslim community in Medina.

The Banu Nadir began rousing the nomads of Najd. The Nadir enlisted the Banu Ghatafan by paying them half of their harvest. This contingent, the second-largest, added a strength of about 2,000 men and 300 horsemen led by Uyayna ibn Hisn al-Fazari. The Banu Asad also agreed to join, led by Tulayha. From the Banu Sulaym, the Nadir secured 700 men, though this force would likely have been much larger had not some of its leaders been sympathetic towards Islam. The Banu Amir, who had a pact with Muhammad, refused to join.

Other tribes included the Banu Murra, with 400 men led by Hars ibn Auf Murri, and the Banu Shuja, with 700 men led by Sufyan ibn Abd Shams. In total, the strength of the Confederate armies, though not agreed upon by scholars, is estimated to have included around 10,000 men and six hundred horsemen. In December 626 the army, which was led by Abu Sufyan, marched on Medina.

In accordance with the plan, the armies began marching towards Medina, Meccans from the south (along the coast), and the others from the east. At the same time, horsemen from the Banu Khuza'a left to warn Medina of the invading army.

Muslim defense The men from Banu Khuza'a reached Muhammad in four days, warning him of the Confederate armies that were to arrive in a week. Muhammad gathered the Medinans to discuss the best strategy of overcoming the enemy. Meeting the enemy in the open (which led to victory at Badr), and waiting for them inside the city (a lesson learned from the defeat at Uhud) were both suggested. Ultimately, the outnumbered Muslims opted to engage in a defensive battle by digging deep trenches to act as a barrier. Since Medina already possessed numerous fortress-like houses and rocks in its surroundings, acting as a natural defense, the trench was only dug at certain places. The longest continuous stretch was made on the northern front.

The tactic of a defensive trench was introduced by Salman the Persian. Every capable Muslim in Medina including Muhammad contributed to digging the massive trench in six days. The ditch was dug on the northern side only, as the rest of Medina was surrounded by rocky mountains and trees, impenetrable to large armies (especially cavalry). The digging of the ditch coincided with a near-famine in Medina. Women and children were moved to the inner city. The Medinans harvested all their crops early, so the Confederate armies would have to rely on their own food reserves.

Muhammad established his military headquarters at the hillock of Sala' and the army was arrayed there; this position would give the Muslims an advantage if the enemy crossed the trench.

The final army that defended the city from the invasion consisted of 3,000 men, and included all inhabitants of Medina over the age of 14, except the Banu Qurayza (the Qurayza did, however, supply the Muslims with some instruments for digging the trench).

According to Irfan Shahid, the Muslims adopted the tactic of using trenches from the Persians, possibly via the Ghassanid Arabs who saw their king killed at the Battle of Thannuris in 527 by this tactic. The adopting is reflected in the Arabic word for this battle, khandaq (), which is a borrowing via Aramaic from Pahlavi kandak.

Siege of Medina The siege of Medina began in January 627 and lasted for 20 nights. Adding in the six days of trench-building, the entire operation lasted 27 days and was contained within a single month (5 Shawwal-1 Dhu al-Qi'dah). Since sieges were uncommon in Arabian warfare, the arriving Confederates were unprepared to deal with the trenches dug by the Muslims. The Confederates tried to attack with horsemen in hopes of forcing a passage, but the Medinans were rigidly entrenched, preventing such a crossing. Both of the armies gathered on either side of the trench and spent two or three weeks exchanging insults in prose and verse, backed up with arrows fired from a comfortable distance. According to Rodinson, there were three dead among the attackers and five among the defenders. On the other hand, the harvest had been gathered and the besiegers had some trouble finding food for their horses, which proved of no use to them in the attack.

The Confederate army made several other attempts to cross the trench during the night but repeatedly failed. Although the confederates could have deployed their infantry over the whole length of the trench, they were unwilling to engage the Muslims at the close-quarter as the former regarded the latter as superior in hand-to-hand fighting. As the Muslim army was well dug-in behind the embankment made from the earth which had been taken from the ditch and prepared to bombard attackers with stones and arrows, any attack could cause great casualties.

Banu Qurayza The Confederates attempted several simultaneous attacks, in particular by trying to persuade the Banu Qurayza to attack the Muslims from the south. From the Confederates, Huyayy ibn Akhtab, a Khaybarian, the leader of the exiled tribe Banu Nadir, returned to Medina seeking their support against the Muslims.

So far the Banu Qurayza had tried their best to remain neutral, and were very hesitant about joining the Confederates since they had earlier made a pact with Muhammad. When Huyayy approached them, their leader refused to allow him entry.

Huyayy eventually managed to enter and persuade them that the Muslims would surely be overwhelmed. The sight of the vast Confederate armies, surging over the land with soldiers and horses as far as the eye could see, swung the Qurayza opinion in the favor of the Confederacy.

News of the Qurayzah's renunciation of the pact with Muhammad leaked out, and Umar promptly informed Muhammad. Such suspicions were reinforced by the movement of enemy troops towards the strongholds of the Qurayza. Muhammad became anxious about their conduct, and realised the grave potential danger the Qurayza posed. Because of his pact with the Qurayza, he had not bothered to make defensive preparations along the Muslims' border with the tribe. The Qurayza also possessed weaponry: 1,500 swords, 2,000 lances, 300 suits of armour, and 500 shields.

Muhammad sent three leading Muslims to bring him details of the recent developments. He advised the men to openly declare their findings, should they find the Banu Qurayza to be kind, so as to increase the morale of the Muslim fighters. However, he warned against spreading the news of a possible breach of the pact on the Qurayza's part, so as to avoid any panic within Muslim ranks.

The leaders found that the pact had indeed been renounced and tried in vain to convince the Qurayza to revert by reminding them of the fate of the Banu Nadir and Banu Qaynuqa at the hands of Muhammad. The findings of the leaders were signaled to Muhammad in a metaphor: "Adal and Qarah". Because the people of Adal and Qarah had betrayed the Muslims and killed them at the opportune moment, Maududi believes the metaphor means the Qurayza were thought to be about to do the same.

Crisis in Medina Muhammad attempted to hide his knowledge of the activities of Banu Qurayza; however, rumors soon spread of a massive assault on the city of Medina from Qurayza's side which severely demoralized the Medinans.

The Muslims found themselves in greater difficulties by day. Food was running short, and nights were colder. The lack of sleep made matters worse. So tense was the situation that, for the first time, the canonical daily prayers were neglected by the Muslim community. Only at night, when the attacks stopped due to darkness, could they resume their regular worship. According to Ibn Ishaq, the situation became serious and fear was everywhere.

Muslim response Immediately after hearing the rumors about the Qurayza, Muhammad had sent 100 men to the inner city for its protection. Later he sent 300 horsemen (cavalry was not needed at the trench) as well to protect the city. The loud voices, in which the troops prayed every night, created the illusion of a large force.