It’s not intended to be a deterrent. It a punishment.
It can be a punishment and a deterrent at the same time with no conflict.
Obviously, not the way it is done today.
But back when naval piracy was rampant in the 18th Century, societies made a concerted effort to prosecute it, punishment was swift and severe.
Up where I live, they have an island in the harbor (as many did, I believe) where the bodies of pirates were covered with tar and hung in a cage where all ships had to pass it entering or leaving.
But piracy did largely disappear over time except in scattered places today where it still exists.