In 1277 Llewelyn was forced to come to terms in the treaty of Aberconwy and pay homage after Edward I defeated him, but the peace did not last long. Dafydd had been settled on territory in North Wales, and made peace with Llewelyn, and became just as unhappy with the officials of the king. In March 1282 he attacked Hawarden castle and captured its English commander. This provoked other rebellions along the border of Gwynedd, and Llewelyn was forced to mobilise or lose his authority. This time when Edward assembled his armies he was determined not merely to bring Llewelyn to heel, but to completely disinherit him. What began as a 'just war' against a people unfaithful to the king, became a war of conquest.
Once again Edward mobilised three armies, but on a much larger scale than in the previous war, and men from Gower served again in the south. Possibly while they were away, Rhys ap Maredudd launched a surprise attack on Gower, burned and sacked Swansea, and laid siege to Oystermouth Castle. When the castle fell, it and village were also burned and the church looted. Ships from Swansea were also used to bring supplies to the north. As Edward's armies closed in on Snowdonia, Llewelyn broke out southwards towards Builth in an attempt to rally support from the south. It was a tragic move, as he met a small band of soldiers outside Builth and was killed on 11th December 1282.
The war continued for several months, until Llewelyn's brother Dafydd was betrayed by his own people and handed over to the English, who executed him in 1283. Edward linked the advance of his armies with castle building to secure the territory he captured and provide for containment of the Welsh once peace was restored. Some castles had been built or remodelled in 1277, but many more were built from 1283. The cost was enormous - £90,000, and the total cost of the two wars and the castles was almost £175,000, a sum equivalent to over one billion pounds today. New boroughs were laid out around each castle, and the best land given to faithful Englishmen. The creation of these castles totally changed the balance of power and allowed Edward to control north Wales in particular.
Llewelyn had no heir and his daughter was sent by Edward to a nunnery. Without leadership, the internal rivalries of the Welsh lords rose to the surface, and they were easily conquered individually. The Statute of Rhuddlan was issued by Edward on 19th March 1284, to lay out the governance of Wales. Welsh territories were converted to English shires, the main ones being Flint, Anglesey, Caernarfonshire, Merionethshire, Cardiganshire and Carmarthenshire. Several new Marcher lordships were also created, such as Chirk, Denbigh and Ruthun. The process of introducing the English justice system was begun by appointing a justiciar for Wales, Robert de Tibotot.