Sheikh Abu bakr al-Waasiti, may Allah be Merciful to him, mentioned that a good action is like fine glass. Just as a little heedlessness will cause it to shatter, after which it will be irreparable. In the same way, good actions are shattered by ostentation and boastfulness, thus it will not accrue any reward.
When a person senses any show and/or ostentation in a work/deed he does, he should try his level best to eradicate it. If it cannot be eradicated, he should not abandon the action/deed, but seek forgiveness from Allah Ta'ala, who could grant him the ability to be sincere. Here, we may cite an incident where a servant doubted his sincerity after constructing an inn for travellers.
Someone told him in a dream that even if his action was insincere, the Du'a of the travellers who will benefit from his noble action will surely be sincere and accepted in your favour. He became extremely pleased and satisfied with this statement.