I flagged a question thinking that it deserved it, but later I realised that the flag wasn't needed. So my question is can we undo a flag?
As far as I know, no, you can't. But there aren't really any consequences to casting a flag that turns out not to be needed. If the flag is declined, you might see a notice next time you cast a flag that prompts you to look at why it was declined, but that's about it.
People who consistently and frequently cast invalid flags may face some consequences, but you'd really have to be trying to run into that situation.