Two weeks ago five young people died and a further five were sent to hospital after taking the drug ecstasy at an electronic music event.
The judge said city authorities should train up inspectors and security guards to police the clubs.
He said little had been done to control the consumption of drugs in clubs.
Judge Roberto Gallardo said there was "a landscape of impunity and lack of state control with respect to nocturnal activities".
He said that the city government of Buenos Aires should establish a plan of action for its inspectors and police forces to enforce laws prohibiting the consumption in public of drugs.
The judge said he wanted to see a timetable of inspections and a plan of action "for the short-, medium- and long term" .
Until this was done, he said, "all dancing to recorded or live music was banned until the order was carried out."
The head of the Buenos Aires government, Horacio Rodriguez Larretta, said he disagreed with the judge's decision:
"There are thousands of people who enjoy themselves in a healthy way every night and we are going to defend them.
"We are asking the judiciary to revoke the ban so we can lift this suspension today."
"We are concerned with addiction, but this doesn't mean we literally have to close down the whole of the city's nightlife."
A nightclub owners association, the Chamber of Dance Venues in Buenos Aires, said they would defy the ban which they described as "unconstitutional".
A judge in the Argentine capital, Buenos Aires, has banned night clubs from opening until they have set up proper health and safety systems.