You're on the right track. It's almost always due to boredom. First, they need to have plenty of room. Overcrowding is the number one reason. Second, they need to have something to keep them busy. Do a websearch for chicken or poultry enrichment, and you'll find all kinds of ideas. Here are a few: hang a head of romaine from a rope, for them to pick at, give them plenty of deep, clean bedding to scratch around in, evergreen boughs to hide in and pick at, baffles or sight barriers in the coop to allow for hiding places, things to jump over or go around, a flake of nice green leafy hay or alfalfa, I use a hay rack to keep it up off the floor, a handful of mealworms thrown in the litter or in a big shallow tub, with some litter, for them to scratch around for, or better yet, a few dozen crickets from the pet store. Scatter dummy eggs around, for them to peck at and realize that there is no benefit to it. All of these things need to be changed frequently, to prevent them from getting bored with the same old thing.
It's true that once started, it is very hard to control, and one bird can soon teach it to all of the others. If you catch it early, before it becomes an ingrained habit, you can correct it, but once it's become a habit, it is extremely difficult to break it and the only answer is to cull. You will not ever be able to introduce new birds to the same flock, because they too will learn it. Like most things, it is much easier to prevent in the first place.