My hamster, Mavi, does this all the time! At first, I was concearned about it too but was able to relax once I found out it was okay. Surprisingly, many hamsters do this and it doesn't spoil the food either.
Their "waste" actually still holds some nutrients and hamsters have been known to actually EAT IT when their food needs aren't being met. :/ Anyway, their food should still be fine but I totally understand if you feel the need to throw out certain foods that seemed spoiled because of this habit.
Hope this helped!