In a frenzied February speech in which he vowed to wipe out rebels like “cockroaches,” Moammar Gaddafi held up China’s 1989 military assault on Tiananmen Square as an example of how to deal with popular unrest.

“Students in Beijing protested for days near a Coca-Cola sign,” he said. “Then the tanks came and crushed them.”

 China’s ruling Communist Party, usually quick to publicize tributes from foreign leaders, banned all reference to the Libyan leader’s tirade in the Chinese media and blacked out foreign television coverage of his praise for the “Tiananmen solution.”

Today, the fortified Tripoli compound where Gaddafi declared his intention of mimicking China’s methods is overrun by rebels. And Beijing is scrambling to distance itself from a leader who lauded its approach to dissent and awarded Chinese companies billions of dollars in contracts — but who has for years also embarrassed, unsettled and sometimes defied the Chinese leadership.

Though often in sync with Gaddafi’s diatribes against Western “imperialism” and eager for a piece of Libya’s massive oil and gas reserves, Beijing has long looked askance at his erratic government, which flirted with Taiwan, criticized China for “colonial” behavior in Africa and frustrated the expansion plans of a big state-owned Chinese petroleum company.

Responding to the collapse of Gaddafi’s rule this week, the Chinese Foreign Ministry said curtly that Beijing respected “the choice of the Libyan people.” It has not formally recognized rebel forces as Libya’s new masters but has clearly abandoned Gaddafi.

The six-month-long battle to unseat the Libyan autocrat confronted China with a now recurrent quandary: how to balance its oft-stated but increasingly threadbare doctrine of non-interference in the affairs of other nations with its own economic and political self-interest.

“Gaddafi criticized China and did not respect China, but there was still economic cooperation between China and Libya,” said Yin Gang, a researcher with the West Asian and African Studies Institute of the Chinese Academy of Social Sciences.

Now that Gaddafi has lost control, Yin added, China will accept whatever new order emerges in his place.

China’s Commerce Ministry has already called on post-Gaddafi Libyan leaders to “protect the interests and rights of Chinese investors.” The plea followed a warning from an official with the rebels’ oil company that China and Russia could lose out in petroleum deals under a new Libyan government because they had shown little support for the anti-Gaddafi rebellion — a stance that in China’s case reflected the tension at the heart of its diplomacy.

Throughout the uprising, Beijing voiced wariness of the West’s intentions and worried about the fate of Libyan contracts that it has valued at $18.8 billion. But it also proved unwilling to go to the mat for Gaddafi. It acquiesced in a U.N. resolution in March that endorsed the use of force, abstaining in a crucial vote that opened the way for intervention by NATO. 

China later criticized NATO airstrikes, and state-controlled Chinese media emphasized the threat of chaos rather than Gaddafi’s brutal rule. Still, Beijing took no action to derail the West’s military campaign in support of rebel forces. 

China has made similar calculations in Sudan, where it for years nurtured close ties with the government in Khartoum and secured access to Sudanese oil fields for the China National Petroleum Corp. (CNPC). Beijing shunned separatists in the south of Sudan, where much of the oil lies, and provided arms to Khartoum to fight them.

But with South Sudan now an independent nation, Beijing has dropped its hostility and is working to cement ties with the former rebels who have assumed control of major oil fields. Foreign Minister Yang Jiechi visited Juba, the capital of South Sudan, in July and hailed a “bright future” in relations.

China is a relatively modest player in Libya’s oil sector, which is dominated by European companies such as Italy’s Eni, but it had hoped to boost its role there as part of a push to feed a growing appetite for imported energy.

CNPC helped build a pipeline in Libya and won a raft of exploration contracts from Gaddafi’s regime. But it reported no major finds. CNPC also suffered a setback in 2009 when authorities in Tripoli torpedoed its effort to buy a Canadian company with sizable oil assets in Libya. Gaddafi’s government took control of the Canadian company’s concessions.

According to Yin, the Chinese researcher, Libya’s most promising oil fields “were all given to Western countries” and China got mostly the dregs.

“Gaddafi answered to the wishes of Western nations, but now the Western world does not like him and the Arab world does not like him, either,” Yin said. “They want to see him gone.”

When the anti-Gaddafi unrest began, CNPC reported attacks on several of its facilities in Libya. Along with other Chinese companies, CNPC pulled out its staff. More than 36,000 Chinese, mostly construction workers, fled Libya to escape the chaos. 

 China initially scorned the rebels’ Transitional National Council but, after Russia embraced the rebel cause, announced in late May that it would “keep contact with all sides in Libya.” The head of the council, Mahmoud Jibril, visited Beijing in June.

When rebels advanced into Tripoli this week, the People’s Daily, the party’s official organ, ran scant coverage of the tumultuous events in the city and  focused mainly on the perils of Western meddling. One article suggested that Western oil interests lay behind Gaddafi’s ouster.

“Famous People in Africa Condemn the Gunboat Policy of the West,” read a Friday headline in the People’s Daily. 

But less tightly controlled media outlets have provided detailed and mostly factual accounts of events. Liberal newspapers denounced Gaddafi and even celebrated his departure.

“He was too much addicted to his own power,” said the Southern Metropolitan Daily. “Many promises he made turned out to be lies.”

Researcher Wang Juan in Shanghai contributed to this report.