How do I reattach a shelf to the wall when it ripped out of the wall?
It was installed incorrectly.

It appears what you have in the picture is a knotting anchor. Near the tip of the plastic sleeve is a threaded section. Properly installed, tightening the screw should pull the far end of the sleeve up against the wall, making a thick "knot" that prevents the anchor from pulling through the wall. Since your failed anchor looks smooth and undeformed, this evidently did not happen.

Here's an example from NPA:



You say there are wooden studs where the anchors were installed. Perhaps you drilled a hole large enough for the anchor then pushed it into the hole in the stud. Since the body of the anchor was then constrained by the surrounding wood rather than in the empty space behind the wall it was unable to expand. Possibly you also did not tighten the screws with sufficient force.

When mounting something where there is a wooden stud, the proper procedure is to not use an anchor at all, but just run a wood screw into the stud. The holding power of a wood screw in wood is a lot more than any hollow wall anchor.