CAIRO — With bulldozers and dynamite, the Egyptian Army on Wednesday began demolishing hundreds of houses, displacing thousands of people, along the border with Gaza in a panicked effort to establish a buffer zone that officials hope will stop the influx of militants and weapons across the frontier.

The demolitions, cutting through crowded neighborhoods in the border town of Rafah, began with orders to evacuate on Tuesday and were part of a sweeping security response by the government of President Abdel Fattah el-Sisi to months of deadly militant attacks on Egyptian security personnel in the Sinai Peninsula, including the massacre of at least 31 soldiers last Friday.

That assault was the deadliest on the Egyptian military in years, and a blow to the government, which has claimed to be winning the battle against insurgents. The resort to a harsh counterinsurgency tactic — destroying as many as 800 houses and displacing up to 10,000 people to eliminate “terrorist hotbeds,” as Mr. Sisi’s spokesman put it — highlighted the difficulties the military has faced in breaking the militants as well as the anger that operations like Wednesday’s inevitably arouse.

“Our house in Rafah is more than 60 years old,” Hammam Alagha wrote on Twitter on Tuesday, detailing his family’s eviction in a series of widely shared posts. After an army officer told the family to evacuate — and Mr. Alagha said he refused — the officer “said tomorrow we will bomb it with everything in it.”