Narrated Abdur-Rahman bin Samurah: The Prophet ﷺ said to me: Abdur-Rahman bin Samurah, when you swear an oath and consider something else to be better than it, do the thing that is beter and make atonement for your oath. Abu Dawud said: I heard Ahmad (b. Hanbal) permitting to make atonement before breaking the oath.
Related hadith on this topic
Explanation & Benefits
Shaykh Umar Farooq Saeedi
Benefits and Issues:
If someone has taken an oath, but there is a Shari‘ah-based or moral benefit in acting contrary to it, then one should act upon the better course.
And the expiation (kafarah) for the oath should be given.
And there is flexibility in this matter: one may give the expiation before or after (breaking the oath).
Source: Sunan Abu Dawood – Commentary by Shaykh Umar Farooq Saeedi, Page: 3277