The apparent failure of some to learn from their punishments does not necessarily mean the punishments failed.
More than one dose or a stronger dose might have been required.
Although individuals may not be deterred by being punished other potential offenders might. This was certainly the case at my school. (General deterrence).
Deterrence is not black and white. Measures that reduce but do not totally eliminate all offending are often worth using.
Punishment is not used soley as a deterrent. For example, it may placate victims and satisfy their demand for revenge in a controlled and moderate way.