The Siege of Niemcza (Polish: Obrona Niemczy) took place during three weeks in August 1017, in the last phase of the German–Polish War (1002–18), when the forces of the Emperor Henry II besieged the town of Niemcza controlled by the Polish ruler Bolesław I the Brave. Despite the aid of Bohemian and Lutici allies, the Imperial attack was ultimately unsuccessful, according to medieval chronicler Thietmar of Merseburg due to the arrival of reinforcements which managed to break into the city and the illness among the German forces. The failure of the siege marked the end of Henry's campaign in Poland and led the emperor to agree to the Peace of Bautzen in 1018, which left the eastern March of Lusatia and the Milceni lands (later Upper Lusatia) under Polish control.
The former Bohemian fortress of Niemcza, located north of the Sudetes range on an important trade route from Prague to Wrocław, had been conquered by the late Polish duke Mieszko I during his campaigns into Silesia about 990. He and his son Bolesław I enjoyed amicable relations with Emperor Otto III, whom Bolesław supported in the battles against the Slavic Lutici tribes, as well as internal struggles within the Empire. Both rulers also attempted to carry out the Christianization of the pagan Prussians. The friendship led to the Congress of Gniezno in the year 1000, during which an autonomous Polish Archbishopric at Gniezno was established, and possibly, Otto acquiesced to Bolesław becoming a king.
However, Otto III died soon after in 1002. In the ensuing power struggle of the royal election for the leadership of the Holy Roman Empire, Bolesław supported his relative (possibly half-brother) Margrave Eckard of Meissen against Henry II. At the same time, the Polish ruler took advantage of the German interregnum to invade and tried to incorporate Bohemia, Moravia, Slovakia, as well as Lusatia and the Margravate of Meissen into the Polish state. After Eckard was killed and Henry II was elected King of the Romans in June, Bolesław participated in the homage of the Saxon estates in Merseburg. However, he only could obtain the March of Lusatia and the eastern Milceni lands of Meissen, while the margravate passed to Eckard's brother Gunzelin. Moreover, Bolesław was attacked when he left Merseburg and narrowly escaped with the help of Margrave Henry of Schweinfurt and Duke Bernard of Saxony. Any involvement of Henry II in the assault remained uncertain, nevertheless Bolesław had Strehla Castle burnt down in revenge and the way to battle was opened.
In the ongoing fight, the Polish duke relied on the support of Henry of Schweinfurt and the Saxon nobility, while King Henry II forged an alliance with the Slavic Lutici tribes. In 1003 Bolesław also took the occasion to invade Bohemia, then shaken by internal struggles of the ruling Přemyslid dynasty. The German king realised that the Polish advance could pose a serious threat to his rule; he concluded an alliance with the pagan Lutici and campaigned Bohemia and the Milceni lands. Bolesław puled back, his ally Henry of Schweinfurt submitted to the king in 1005. After several years of fighting, a temporary peace was reached in 1013, when Bolesław and Henry II again met in Merseburg and the Lotharingian princess Richeza of Lotharingia was married to Bolesław's son Mieszko II.
Intense fighting again erupted, after the Polish ruler refused to accompany Henry II to his 1014 Imperial coronation in Rome. The emperor insisted on Bolesław's duties as a vassal; he demanded repentance and even had Mieszko II arrested. The Saxon nobles enforced Mieszko's surrender, however, Henry went on a campaign into Poland while Bolesław was stuck in the Kievan struggle of his son-in-law Sviatopolk I. Repulsed in 1015, the emperor's forces suffered high losses but returned two years later, after peace negotiations failed. A great Imperial army left Magdeburg, reaching the Oder river at Krosno. Under constant attacks by Bolesław's forces, Henry II turned southeastwards and passed Głogów to encounter the Polish army at the large fortress of Niemcza in August 1017.
Henry II reached the place slightly earlier. He received support by the troops of Duke Oldřich of Bohemia as well as by his pagan Lutici allies who attacked the Polish territory from the west. While Bolesław's men entrenched themselves, the emperor laid siege to the Niemcza fortifications but was unable to supply the besieging force with food for a long time. Several attempts to storm the fortress failed and Henry finally had to withdraw. As Bolesław's forces were stationed behind him at Wrocław, the emperor was forced to make his way back into Germany south across the Sudetes through Bohemia. However, at the same time Boleslaw's son Mieszko was conducting a campaign in the Bohemian territory which made even this route difficult for Henry's army. To further complicate matters, a dispute arose between Henry's Christian Saxon troops and the Lutici auxiliaries when the Christian knights insulted a sacred pagan statue of the latter.
The conduct of the siege, as well as the more general campaign of Henry II during 1017 was described by the German chronicler Thietmar of Merseburg. The failure after 15 years of fighting forced the emperor to agree to the Peace of Bautzen which left the Milceni lands and Lusatia under Polish control. Emperor Conrad II reconquered the territories in 1031.
Bohemia remained a Přemyslid duchy under control of the Holy Roman Empire. At the 1054 Diet of Quedlinburg, Emperor Henry III officially ceded the Silesian territories to Polish rule against the payment of an annual tribute to the Bohemian dukes. Nevertheless, in 1093 another attempt by Duke Bretislav II to occupy Niemcza failed. The Polish possession of the town was solemnly confirmed in a peace treaty on Pentecost 1137 between Duke Soběslav I of Bohemia and the Polish duke Bolesław III Wrymouth.