Madina had natural defenses on three sides. Only the fourth side was vulnerable to attack. It was decided that a ditch should be dug on this side. The site for the ditch was demarcated by the Holy Prophet. All male adults of the Muslim community in Madina were mobilized to dig the ditch. The labor force was divided into batches and each batch had a leader. Each batch was required to dig a ditch ten yards long. Ali led one batch. The Holy Prophet, Ali and all other companions worked day and night as laborers to dig the ditch. The Muslims labored to the rhythm of a war song they sang:
By God, had not Allah guided us, we would not have been on the right path. We would not have given any Sadaqa nor offered prayer. May Allah bestow on us confidence and calmness of mind. May He make our steps firm to face the enemy. The enemy has risen against us and seeks our destruction. Allah, we seek your protection. There is no welfare except that in the next world; Allah, shower your grace on the Ansars and the Muhajjirs." These verses were composed by Ali.
These verses inspired the Muslims to action, and the entire stretch of the ditch was completely dug within twenty days. The ditch was five yards deep and five yards broad. Each batch of the Muslims who had dug the ditch was required to stand guard over the ditch in their part of the sector. In each sector there were archers who shot arrows to keep the enemy away from the ditch.