Mayor Bill de Blasio on Tuesday declared the end to a months-long public health emergency over an outbreak of measles in Brooklyn that infected 654 people since October 2018.

The outbreak has ended because no new cases of the highly contagious virus have been reported since July, according to health officials. But the mayor still urged caution.

“As we head back to school this week, we just remain vigilant. To keep our children and communities safe, I urge all New Yorkers to get vaccinated. It’s the best defense we have,” de Blasio said in a statement.

Only unvaccinated children with valid medical exemptions will be allowed to return to school following a recent state law prohibiting personal and religious exemptions.

On April 9, the mayor ordered mandatory vaccinations enforced by the threat of $1,000 fines in Brooklyn neighborhoods where the outbreak was concentrated. City officials handed out 232 summonses to people for violating the vaccination order and closed 12 schools. Only seven summonses resulted in penalties. The majority were canceled after the individuals provided proof that they’d already received the shots.

Over 15,000 people received vaccinations since April — a 41 percent increase from the same period last year, according to city health officials.

The city spent $6 million combating the outbreak that resulted in 52 hospitalizations. It was the largest measles outbreak in nearly three decades.

“We are fortunate that no one died as a result of being exposed to measles but many of these hospitalizations were serious, some requiring intensive care unit care. They all could’ve been easily prevented by vaccinations,” said city Health Commissioner Dr. Oxiris Barbot at a press conference Tuesday that was not attended by the mayor.

Vaccination rates in the city increased by 27 percent since last fall, Barbot said. She added that there’s little chance of the outbreak returning.

“What we have working for us is that we have dramatically decreased the number of individuals who are not immunized and so the likelihood of having ongoing community transmission to the degree we have recently experienced is pretty low,” Dr. Barbot said.