Don’t let the warnings you’ll occasionally see online fool you—onions can be composted with just a few precautions. The reason some people recommend not composting onions is that they have such a strong odor that they can attract pest animals if they’re tossed on top of the bin carelessly.
The truth is that all leftover foods have the potential to bring in pests (though bread products and smelly foods like onions are the most attractive to wildlife), and the extra steps you take with onions should be taken with all leftover food that you add to the compost pile.
Just make sure to bury the onions down in the heap of compost so they’re not sitting on the surface of the pile, and close the lid before walking away. Some sources say you should bury the onions at least 10 inches into the compost pile to ensure the smell doesn’t escape. You should also chop onions up before adding them to your compost so that they break down in a timely manner.
It can take quite a while for a whole onion to decompose without a little help from you, so chop them roughly to help things along. (In fact, you should break any large items you’re composting down into smaller bits to help the decay process move along more quickly.)
The only circumstances that make onions off limits are if you’re vermicomposting (composting with a worm farm). Because onions have such a pungent smell, the worms are not interested in eating them, so they can take much longer to decompose than the other items that make up the compost in a vermicomposting setup. (The same rule applies to other smelly foods, such as broccoli, garlic, and even potatoes—worms just aren’t interested in eating these foods, so skip them if you’re working with a vermicomposting arrangement.)