NEW YORK — They beat the beetles. An invasive tree-killing bug has finally been cleared from New York City's forests after a two-decade battle, officials announced Thursday.

The Asian longhorned beetle, a wood-boring insect that has plagued the city since the late 1990s, has been eradicated from the five boroughs, the city's Parks Department said. The declaration of victory came nearly a decade after the last confirmed city sighting of the bug in 2010, according to parks officials.

The beetle "was one of the greatest insect threats our city has ever faced, and I would like to give thanks to all of our staff and partners for their hard work dedicated to removing this invasive species," First Deputy Parks Commissioner Liam Kavanagh said in a statement. The East Asian bugs crawl deep into trees as larvae and then kill the trees by burrowing their way out as adults, the parks department said.

They attack hardwood trees such as maples, elms, willows and poplars and can cause millions of dollars in damage to commercial forests, officials say. The beetles were found in Norway maple trees in Brooklyn in 1996, likely after coming over in packing material from China, according to the state's Department of Environmental Conservation.

The eradication effort that started late that decade led Manhattan and Staten Island to be declared beetle-free in 2013, the parks department said.

A quarantine was imposed on northern Brooklyn and Queens that year to keep wood from infested areas from spreading, officials said. The parks department scheduled pickups of wood debris from homes in infested parts of the city and helped monitor trees on public property, according to the agency.