WASHINGTON — Conditions in California’s overcrowded prisons are so bad that they violate the Eighth Amendment’s ban on cruel and unusual punishment, the Supreme Court ruled on Monday, ordering the state to reduce its prison population by more than 30,000 inmates.

Justice Anthony M. Kennedy, writing for the majority in a 5-to-4 decision that broke along ideological lines, described a prison system that failed to deliver minimal care to prisoners with serious medical and mental health problems and produced “needless suffering and death.”

Justices Antonin Scalia and Samuel A. Alito Jr. filed vigorous dissents. Justice Scalia called the order affirmed by the majority “perhaps the most radical injunction issued by a court in our nation’s history.” Justice Alito said “the majority is gambling with the safety of the people of California.”

The majority opinion included photographs of inmates crowded into open gymnasium-style rooms and what Justice Kennedy described as “telephone-booth-sized cages without toilets” used to house suicidal inmates. Suicide rates in the state’s prisons, Justice Kennedy wrote, have been 80 percent higher than the average for inmates nationwide. A lower court in the case said it was “an uncontested fact” that “an inmate in one of California’s prisons needlessly dies every six or seven days due to constitutional deficiencies.”