You should throw out spaghetti when it develops a bad odor, off smell, mold, slimy texture or tastes bad.
When cooked spaghetti goes bad it will often start growing mold and begin smelling bad.
Cooked spaghetti often lasts around 3 to 5 days in the refrigerator.
If the cooked spaghetti has been left out at room temperature longer than a few hours it could contain bacteria and should be thrown out as it could make you sick.
If you want to keep the cooked spaghetti longer than 3 days you can freeze it in a freezer bag or sealed freezer safe container.
Uncooked spaghetti can last for a few years and sometimes longer as long as it remains sealed and no bugs get into it.
I've used uncooked spaghetti that was 5 years old and it was still safe to eat although it didn't taste as good as it would if fresh.
Uncooked spaghetti is also best eaten before or soon after expiration but it can remain safe to eat for a long time.