CAIRO (AP) — Egypt launched a major shakeup of its security services on Saturday in an apparent reaction to an ambush by militants outside Cairo last week that killed at least 16 police troopers.

President Abdel-Fattah el-Sissi replaced his armed forces chief of staff, while the Interior Ministry, in charge of police, dismissed the head of national security, a handful of generals, and a dozen senior leaders responsible for the area where the deadly shootout occurred.

The move was not unexpected after officials publicly evoked potential intelligence failures, lack of coordination, or incompetence as playing a factor in the losses, the latest installment of Egypt's ongoing war against Islamic militants, including the Islamic State group.

Last Friday's attack took place in the al-Wahat al-Bahriya area in Giza province, about 135 kilometers (84 miles) southwest of Cairo — a gateway into Egypt's vast Western Desert which leads to lawless Libya. Authorities consider the area an infiltration path for smugglers and militants, and have blamed some past attacks on extremists transiting through.

The ambush's brazenness and location raised the specter of the simmering insurgency creeping closer to the capital, which has been largely secure and far from what has long been its main front — a northeastern corner of the Sinai Peninsula.

Security troops have announced major operations in the Western Desert and areas near Cairo in recent days, and foreign companies and embassies have long warned expats against travel to such places, even those as seemingly tame as the Fayoum Oasis.

Just a day ago, security forces killed 13 militants in another gun battle in the western desert, during a raid on an alleged militant hideout in the New Valley province which shares part of the porous border with Libya.

The attack a week ago that seemingly prompted the leadership shakeup appeared to be a carefully planned ambush that trapped many counter terrorism officers as well, with police ranks of those killed reaching up to brigadier-generals.

The troops had been acting on intelligence and moving against a militant hideout backed by armored personnel carriers when they drew fire and rocket-propelled grenades, according to officials who reported the incident anonymously because they weren't authorized to brief journalists.

They added that the force likely ran out of ammunition and that the militants captured several policemen and later killed them.

While the official death toll announced by the Interior Ministry was only 16 at the end of the day, with 15 militants killed or wounded, officials who spoke with the Associated Press earlier had citied over 50 policemen killed in the attack. That would make it one of the worst on Egyptian police in years, although the differing numbers could not be reconciled.

Egypt's categorically pro-government media unleased a tidal wave of jingoism following the killings, calling for military trials and summary executions of militants. And along with some officials, they have accused the foreign media of publishing fake news for reporting the higher death toll.

No one claimed responsibility for the attack, although the IS local affiliate is spearheading an insurgency in Sinai, which borders Israel and the Gaza Strip, and the group has been active recently in neighboring Libya, home to a wide array of militants.

Egypt has been under a state of emergency since IS-claimed bombings and suicide attacks targeting minority Coptic Christians killed scores earlier this year, and attacks on the mainland have recently increased.

The last time Egypt's security forces suffered such a heavy loss of life was in July 2015 when IS militants carried out a series of coordinated attacks, including suicide bombings, against army and police positions in Sinai, killing at least 50. However, the army then said only 17 soldiers were killed along with over 100 militants.

Attacks by militants significantly increased since the army's 2013 ouster of an elected but divisive Islamist president.

Following el-Sissi's decree, Lt. Gen. Mohamed Farid Hegazy will now be the country's second most senior soldier, replacing Lt. Gen. Mahmoud Hegazy. The two are not related.