A mildew odor trapped in carpet fibers not only smells bad, but it could indicate a moisture problem, such as a leak. While household items can get rid of the odor, it's important to check the carpet to ensure there isn't mold growing on it as well, which could be a health issue.
Check for Moisture
Before dealing with the odor, touch the carpet to see if it's wet. Fold a paper towel and press it down onto the area that smells if the carpet isn't visibly wet. If the towel comes back damp, press a fresh paper towel in surrounding areas to check the extent of the moisture problem.
If the carpet is wet or damp and you aren't sure why, check for the source of the moisture, such as a leaking dishwasher or pipe. This issue should be dealt with first in order to prevent further damage to the carpet and floor.
In some cases, the carpet may smell of mildew simply due to the dampness of the area in general, such as in a cottage near a large body of water.
Dry the Carpet
If the carpet is still damp, dry it out as soon as possible to help reduce odor and potential floor damage. Open windows and turn on ceiling fans. Portable box fans can also help dry the carpet quickly. If the carpet is really wet, use a wet/dry shop vac to soak up excess moisture. If only one area is damp, such as from a minor leak, press absorbent rags into the area to remove excess moisture. This will reduce the drying time.
Baking Soda Treatment
Sprinkle baking soda over the entire carpet and allow it to sit for at least several hours. Vacuum the baking soda up afterward to help lift some of the odor out of the carpet. The baking soda can be applied while the carpet is slightly damp, but not soaking wet, as this may cause the baking soda to clump and stick to carpet fibers.
Vinegar and Water Spray
Mix 1 part white vinegar with 2 parts water in a spray bottle, then spray the smelly carpet. Use enough to wet the area but not soak the carpet, as this could damage the floor if it stays wet too long. Allow the vinegar solution to air dry. Use ceiling fans or portable fans to help speed up the process. Open windows as well, if possible, to help get both the mildew and vinegar odor out of the air. Sprinkle the area with baking soda once the carpet is mostly dry, allow it to sit overnight, then vacuum the carpet the next morning. Spray the carpet again with the vinegar solution if the mildew odor is still obvious after a day or so.
Vinegar and Water Carpet Cleaning Solution
Steam cleaning the carpet will also help remove any lingering odors. Mix equal parts white vinegar and warm tap water, then pour it into the liquid chamber of a carpet steamer. A few drops of a citrus essential oil can be added to the liquid if you prefer a fresh scent additive. Clean the carpet according to steamer directions, which may vary by brand.
If the carpet has been wet or even underwater for some time, there's a good chance mold is present. It may be growing on the underside or on the carpet padding and not otherwise visible on the surface. If you suspect the carpet is moldy, it most likely needs to be replaced. If unsure, lift up a section of the carpet and look underneath. If necessary, slice the carpet in an inconspicuous area to make a flap so you can check the padding. If you see spotty black, white, gray or green spots, it's likely mold. Contact a professional to remove the carpet and to treat the area for mold.