What about guilt?

I think I see in this Q&A that feelings such as shame, embarrassment are indulgent and therefore unnecessary. I wanted to ask about guilt. Is is like shame, an indulgent emotion that we can change our thinking about? Or is it a necessary emotion, at least sometimes–one that says, I shouldn’t have done that and I will do better next time? Can it be both?

I can see indulging in guilt, i.e. beating myself up over something that I can no longer change, but I can also see it as a useful warning signal that I’m not acting in accordance with my best self.

What say you?