There are many different worms that create cocoons. Cocoons protect insects form predators and the environment and provide shelter. They are often made of silk with the size of the cocoon dependent of the size of the insect that created it. Following is a brief list of common types of cocoon worms:
Silk worms are often confused for worms, but they are not a member of the worm family at all. Instead, they are caterpillars that create a cocoon to complete the transformation process from caterpillar to butterfly. Silk worms create their cocoons after being alive for one month. It then takes three days for them to build a cocoon that they will live in for three weeks before emerging as a butterfly.
Earthworms create cocoons to protect their young offspring and embryos as they develop. The cocoon is created during the mating ritual and then buried in the ground until the offspring are ready to emerge.
Frequently used for composting, red worms create cocoons that are oval in shape and dark in color. These cocoons are created to protect eggs during the development stage of life. They are very strong and can last for several years protecting future generations from the elements. There have been many cases where red worm cocoons hatched young worms years after being created during the mating process.
Tomato Hornworms are closely relation to Tobacco Hornworms. They are not worms but do feed on tomato plants and other garden plants voraciously until they are ready to pupate and transform to the next stage of the life. Tomato Hornworms are fat, green worm like creatures that have excellent camouflage abilities; they are very difficult to see during the day as they blend in with the garden environment with ease.
Tomato Hornworms are really moths and they are garden pests of the worst variety as they ruin plants and destroy crops. Sometimes, these worms are seen with white cocoons on their backs. These cocoons should not be destroyed as they are the cocoon of the Braconid Wasp, a predator of the Hornworm. These wasps will help eliminate the Hornworms from your garden, not create more.
There are many creatures such as butterflies and beetles that are mistaken for worms during their early life development. True worms that make cocoons do so during the mating process as the cocoon is to protect eggs during development. In general, if you see a worm like creature creating a cocoon then it is an insect of another name.