The Messenger of Allah ﷺ said, "If a plague breaks out in a land and you are not in it, do not enter it, and if it breaks out in a land while you are in it, do not leave it."
حدثنا يحيى عن شعبة حدثني حبيب بن أبي ثابت عن إبراهيم بن سعد قال سمعت أسامة بن زيد يحدث
قال رسول الله ﷺ إذا كان الطاعون بأرض وأنتم ليس بها فلا تدخلوها وإذا كان بأرض وأنتم بها فلا تخرجوا منها
Its chain of transmission is sahih. It was previously mentioned in 21695.