If the bugs have spread, you may need to throw out everything that isn't in a can or glass. If the infestation isn't too severe, place things such as flour and baking mixes in the freezer for 24 hours to kill larvae. Wipe all shelves and containers with a solution of 1 teaspoon bleach in 1 quart of water. If you need to use an insecticide, make sure it is labeled for pantry use. Transfer things such as flour, sugar, rice and pastas into airtight containers.
Opaque containers preserve food better than clear ones.
Use resealable bags to store opened boxes of brown sugar and confectioner's sugar. Store bags of cornmeal, whole grains and flours you don't use often, like whole wheat and rye, in the refrigerator, which will also keep them from getting rancid. Make sure you've used the old batch before you open a package. Keep a marking pen in the pantry or cabinet so you can scribble dates on things. Watch where you store things.