What you might think is worms in your carpet is most likely the larvae of assorted insect species, such as spider, moth and fly larvae. No matter what type of larvae you are dealing with, it is best to remove them as quickly as possible through meticulous cleaning. These problems often arise from poor cleaning habits; therefore, intense daily cleaning will eradicate the infestation fairly quickly. If you find you cannot shake the infestation despite your best cleaning efforts, it may be time to shop for a new carpet.
Sprinkle diatomaceous earth over carpeting to kill larvae. Diatomaceous earth is the skeletal remains of diatoms, an ancient form of algae. Although the stuff is nontoxic to humans and other mammals, it is deadly to insects and worms, as the earth's sharp edges cut into insects and their larvae and kill them.
Vacuum the carpet thoroughly, including under all furniture. You may want to vacuum couches or bedding as well to avoid infestations. Vacuum the carpeting every day, if not several times a day, until the problem has been resolved.
Remove furniture and shampoo the carpet using carpet shampoo and hot water, which will kill the larvae more easily than cold water. Shampoo the carpet at least twice a week until the problem subsides.
Vacuum the carpet again after shampooing when it is dry to remove dead larvae. Throw out the vacuum cleaner bag after use to avoid having larvae crawl out of the vacuum and back onto your carpet.