The lights no longer work in the sports hall of Patton Barracks, so building manager Heiko Mueller uses bricks to prop open the doors and let in the sun. It reveals basketball nets with fraying fibres hanging precariously from the walls, blue gym lockers scarred with rust, and mould growing on the shower room floor. The whistle blew on the hall’s last basketball game five years ago.

For nearly 70 years after the second world war, Heidelberg was the US army’s headquarters in Europe, and a Nato command centre. But in 2009 the Pentagon decided to reduce the number of American troops in Europe, including pulling out of the German city entirely. By September 2013, they were all gone.

There was a lot of fear when the Americans moved out Carmen James

Their departure stripped Heidelberg of a significant chunk of its identity. It had long been known for its 700-year-old university and 800-year-old castle, but the link with the army had become inescapable: 20,000 soldiers and their associates had lived in a city of only 150,000 people, occupying more than 180 hectares of prime land – roughly the same size as the city’s historical centre.

“There was a lot of fear when the Americans moved out,” says long-term Heidelberger Carmen James. “They were a big employer and part of our way of life.” The mayor, Eckart Wuerzner, predicted the withdrawal would cost the city €50m (£45m) each year, and even flew to Washington DC to persuade the US to change its mind, in vain.

The rotting Patton Barracks basketball court. Photograph: Matt Pickles

The army’s departure did indeed lead to job losses, and to a fall in trade for shops, restaurants and even energy providers. But over time, the city began to realise that the space left by the army wasn’t just a disaster, but a potential opportunity.

Heidelberg’s university ranked highly for medical and life sciences, and was home to the software multinational SAP. But new graduates would regularly leave for better jobs elsewhere, and the city’s nascent technology sector was having trouble getting off the ground, because it lacked space – for research to be spun out into companies, for startups to expand, and for employees to live affordably.

We are in the perfect location to connect the Googles of the west with the Alibabas of the east Eckart Wuerzner

The departure of the US army changed all of that. One early victory came when a rising young company, Ameria, which develops digital shop floors, was considering leaving – until it was offered space in the former officers’ casino of Patton Barracks. The new digs suited it, and in 2021 it will move into new offices that connect to pop-up shops where it can test ideas on customers.

“There was no space like this in Heidelberg, or anywhere really,” says Ameria’s Johannes Troeger. “Innovation needs space, and the former Patton Barracks are the space to create a vibrant community of startups, established companies and corporations.”

Beds in the former officers’ mess at Patrick Henry Village refugee centre, which once housed 16,000 soldiers. Photograph: Ralph Orlowski/Reuters

The US withdrawal also came just before the global migrant crisis, when hundreds of thousands of refugees arrived in Germany. Many cities struggled to accommodate the new arrivals – but Heidelberg had Patrick Henry Village, a 100-hectare site that once housed 16,000 soldiers.

It became the registration centre for all refugees to the state of Baden-Württemberg. Twice as many refugees have since come through the site than there are residents in Heidelberg, and the city has become a testing ground for solutions to Germany’s integration challenge.

Something seems to be working: fewer than 5% of Heidelbergers consider migration a major problem, and no difference has been observed in school attainment between refugees and locals.

Children play basketball at Patrick Henry Village refugee centre in 2015. Photograph: Ralph Orlowski/Reuters

A project called Weltliga brings together locals and refugees for a free game of football every Tuesday at 3pm.

“Last year we had more than 100 players every week,” says Benedict Bechtel, who runs the programme. Today there are fewer than 20. “Most of the guys are now busy at 3pm,” he explains, gesturing to the game on the artificial pitch behind him. “They are working or taking classes or seeing friends.”

The city’s openness to migration and innovation has convinced an incubator fund that backs refugees’ business ideas to move there from Amsterdam this month. R Ventures Foundation hopes that establishing refugee-led companies will help to change perceptions of refugees from “job stealers” to “job creators”.

“From being known as a city of thinkers, Heidelberg is becoming a city of doers,” says founder Archish Mittal. “I believe it is only a matter of time until it is known globally as the city of innovation.”

That concept has become the cornerstone of Heidelberg’s post-army identity. The city has recently struck partnerships with Palo Alto and Hangzhou, two of the world’s leading tech cities, and attracted three of China’s biggest technology parks to the city.

Nature reclaims a bus stop once used to ferry soldiers around the Patton Barracks. Photograph: Matt Pickles

The mayor’s early fears are gradually giving way to a more bullish optimism. “We are in the perfect location to connect the Googles of the west with the Alibabas of the east,” Wuerzner says.

Fewer than 30,000 American soldiers remain in Europe, and further withdrawals are expected following US president Donald Trump’s comments about Nato contributions from Europe. Not all towns facing a military drawdown have assets like Heidelberg’s university, but the city’s experience shows that withdrawal can be an opportunity not only to build new developments, but a new identity.

Meanwhile, the bulldozers have arrived at Patton Barracks, where over the next two years the bunk beds, casino, discotheque and theatre will be gutted and transformed into the Heidelberg Innovation Park, with new offices and so-called smart city additions such as streetlights that act as wifi hubs and can monitor traffic.

Mueller, the building manager, kicks away the brick holding open the door to the sports hall and locks it. “This is one of the last chances to enter this site,” he says. “And this site is a big chance for Heidelberg.”

Follow Guardian Cities on Twitter, Facebook and Instagram to join the discussion, and explore our archive here