In 1798, the Helvetian Republic was made and the lords from Bern were removed. The canton of Aargau was founded and Lenzburg became the capital of the district in 1803. The canton gained the castle in 1804. Lenzburg became the economic center of the area in the 19th century. In 1875, Lenzburg was added to the railway system. Competition for trade became too much and Lenzburg became bankrupt only one year later. The town recovered from bankruptcy during the next 10 years.