I'm not certain about the conditions which might make building walls around cities necessary or desirable, but I can say that with modern conditions of relative wealth and ease of construction compared to premodern societies building walls around cities would be comparatively easy.
I'm not certain that modern people would be okay with gates where every road crosses the wall and the resulting delays in traffic if each vehicle has to stop at the gate for a check. Of course many old time cities left the gates open all day and let anyone go in or out during the day, and only closed the gates at night. That would be far more convient for modern people than stopping every vehicle for some sort of check as in an international border checking point.
European cities and towns and many villages usually had walls. If a city expanded or contracted its area, new walls were often built encloser larger or smaller areas.
In the USA and many other modern countries, cities have large suburbs which constantly increase and sprad out. City walls which included surburban areas would have to constantly be be expanded.
But if a sity is built with many buildings which are say, a hundred stories tall and a mile wide each, with people living on each floor of each building, or if cities are built with only skyscaper apartment buildings and no vast suburbs of single family homes, the inhabited areas of cities could be much smaller than the sprawling automobile based cities and it would be much more practical to build walls around such cities.
But anyway, building walls around cities would be practical if desired. Premodern Chinese cities were filled with houndreds and thousands of residential compounds, each with a wall around it. And sometimes each neighborhood in a large Chinese city had a wall around it. And the entire Chinse city usually had a wall around it.
And of course the country of China often had a wall around a large part of its permimeter. The Great Wall(s) of China blocked the northern border. I don't know how many thousands of kilometers or miles the Great Wall stretched at any one time, because entirely new great walls were built from time to timeby different governments when the border was in different locations.
So I don't now whether statements about the total length of the Great Wall include the total lengths of walls built and maintained at different times over two thousand years, or only the total length of, for example, the Ming Dynasty walls.
Anyway, ancient and medieval and early modern China had countles thousands of walled household compounds in the country and in cities, and coultless walls within and around cities, and often the most recent version ofthe Great Wall. It was a country with countless thousands of kilometers or miles of walls in total.
If all the other walls in China disappeared, or were never built in the first place, and the only walls that were built and maintained were the big walls around the hundreds of Chinese cities, building and maintaining those walls would have been a lot less work than building and maintaining all the walls which actually did exist in China.
Benin is the name of a country in west Africa. Benin city is in the neighboring country of Nigeria, just to confuse people. And one thing which Benin City is famous for is the Walls of Benin.
The Walls of Benin are a series of earthworks made up of banks and ditches, called Iya in the Edo language, in the area around present-day Benin City, the capital of present-day Edo, Nigeria. They consist of 15 km (9.3 mi) of city iya and an estimated 16,000 kilometres (9,900 miles) of rural iya in the area around Benin.1 The 'walls' of Benin City and surrounding areas were described as "the world's largest earthworks carried out prior to the mechanical era" by the Guinness book of Records.2 Some estimates suggest that the walls of Benin may have been constructed between the thirteenth and mid-fifteenth century CE3 and others suggest that the walls of Benin (in the Esan region) may have been constructed during the first millennium CE.3
The walls were built of a ditch and dike structure; the ditch dug to form an inner moat with the excavated earth used to form the exterior rampart.
https://en.wikipedia.org/wiki/Walls_of_Benin
So if the rural walls outside of Benin city total 16,000 kilometers or 9,900 miles, and were a single wall, they could make a square 4,000 kilometers by 4,000 kilometers, or 2,475 miles by 2,475 miles. But no doubt they are a group of many hundreds or thousands of much smaller enclosures, many of them probably connected.
I note that in Europe many "dykes" were built, which were defensive or boundry earthworks with earthern ditches and walls, similar to the Niegerian lya, except that the ditch was usually on th eouter side and the earthen wall on the inner side.
I note that Schleswig-Holstein in Germany has a defensive dyke called the Danevirke built over many centuries to defend Denmark from invasion from the south. The Danevirke was last used from military purposes during the Second Schlesvig-Holstein War in 1864.
Thus I suspect that European type dykes or Nigerian style lya could still be used for some military or police purposes in the 21st century.
I also note that walls and fences have often been used to keep wild animals or livestock inside or outside specified areas.
One form of livestock containing wall is a ha ha.
A ha-ha (French: hâ-hâ or saut de loup) is a recessed landscape design element that creates a vertical barrier (particularly on one side) while preserving an uninterrupted view of the landscape beyond from the other side.
The design can include a turfed incline that slopes downward to a sharply vertical face (typically a masonry retaining wall). Ha-has are used in landscape design to prevent access to a garden by, for example, grazing livestock, without obstructing views. In security design, the element is used to deter vehicular access to a site while minimizing visual obstruction.
https://en.wikipedia.org/wiki/Ha-ha
In many parts of Africa and South Asia, some farms and vilages have various forms of anti elephant ditches, walls, or fences to keep elephant herds from raiding the crops.
Australia is famous for rabbit proof fences.
The State Barrier Fence of Western Australia,1 formerly known as the Rabbit Proof Fence, the State Vermin Fence, and the Emu Fence, is a pest-exclusion fence constructed between 1901 and 1907 to keep rabbits and other agricultural pests, from the east, out of Western Australian pastoral areas.2
There are three fences in Western Australia: the original No. 1 Fence crosses the state from north to south, No. 2 Fence is smaller and further west, and No. 3 Fence is smaller still and runs east–west. The fences took six years to build. When completed in 1907, the rabbit-proof fence (including all three fences) stretched 2,023 miles (3,256 km). The cost to build each kilometre of fence at the time was about $250 (equivalent to $18,906 in 2018).3
When it was completed in 1907, the 1,139-mile (1,833 km) No. 1 Fence was the longest unbroken fence in the world.4
https://en.wikipedia.org/wiki/Rabbit-proof_fence
The Darling Downs–Moreton Rabbit Board fence is a pest-exclusion fence constructed between 1893 and 1997 to keep rabbits out of farming areas in Queensland, Australia. It is managed by the Darling Downs–Moreton Rabbit Board1 and is often referred to as "The Rabbit Fence" or "Rabbit Board Fence".[2][failed verification]
As of 2021 the fence has been expanded to 555km of rabbit-proof fence running from Mt Gipps to Goombi.1
https://en.wikipedia.org/wiki/Darling_Downs%E2%80%93Moreton_Rabbit_Board_fence
The Dingo Fence or Dog Fence is a pest-exclusion fence that was built in Australia during the 1880s and finished in 1885, to keep dingoes out of the relatively fertile south-east part of the continent (where they had largely been exterminated) and protect the sheep flocks of southern Queensland. It is one of the longest structures in the world. It stretches 5,614 kilometres (3,488 mi)1 from Jimbour on the Darling Downs near Dalby through thousands of kilometres of arid land ending west of Eyre peninsula on cliffs of the Nullarbor Plain2 above the Great Australian Bight3 near Nundroo.4
https://en.wikipedia.org/wiki/Dingo_Fence
There are also barriers built for enforcing laws.
The Inland Customs Line, incorporating the Great Hedge of India (or Indian Salt Hedge1), was a customs barrier built by the British colonial rulers of India to prevent smuggling of salt from coastal regions in order to avoid the substantial salt tax.
The line was gradually expanded as more territory was brought under British control until it covered more than 2,500 miles (4,000 km), often running alongside rivers and other natural barriers. It ran from the Punjab in the northwest to the princely state of Orissa, near the Bay of Bengal, in the southeast. The line was initially made of dead, thorny material such as the Indian plum but eventually evolved into a living hedge that grew up to 12 feet (3.7 m) high and was compared to the Great Wall of China. The Inland Customs Department employed customs officers, jemadars and men to patrol the line and apprehend smugglers, reaching a peak of more than 14,000 staff in 1872.
The line and hedge were abandoned in 1879 when the British seized control of the Sambhar Salt Lake in Rajasthan and applied tax at the point of manufacture.
https://en.wikipedia.org/wiki/Inland_Customs_Line
So I can imagine that even in modern times some sort of ditches, or dykes, or walls, or fences could be built around cities to keep people or animals in or out.
Such defenses would not be very good a stopping or slowing down invasions by major military forces, but could have some degree of police or military function.