"If it is evil in your eyes to serve the LORD" (Joshua 24:15). Joshua did not rebuke Israel until close to his death. And for four reasons one does not rebuke a person except close to death: so that he not have to rebuke him and then rebuke him again; so that his fellow not see him and be shamed before him; so that he not bear it against him in his heart; and so that the one rebuked may depart in peace, for rebuke brings about peace. And so you find with Abraham, "And Abraham rebuked Abimelech" (Genesis 21:25). And so it says of Isaac, "Why have you come to me" (Genesis 26:27). And so you find with Jacob, "And Jacob called to his sons" (Genesis 49:1); to Reuben he says, in effect: For what reason did I not rebuke you all these years? So that you would not leave me and cling to Esau my brother. And so you find with Moses, "And it came to pass in the fortieth year, in the eleventh month" and so forth (Deuteronomy 1:3). And so you find with Samuel, as it is said, "Here I am, testify against me before the LORD and before His anointed" (1 Samuel 12:3). And so you find with David, that he did not rebuke Solomon except close to death, as it is said, "And the days of David drew near that he should die, and he charged Solomon his son, saying" and so forth (1 Kings 2:1).
Why the Righteous Waited Until the Edge of Death to Rebuke
Yalkut Shimoni on Nach 34:1
אם רע בעיניכם לעבוד את ה' (יהושע כד, טו), יהושע לא הוכיח לישראל אלא סמוך למיתתו, ומפני ארבעה דברים אין מוכיחין את האדם אלא סמוך למיתה, כדי שלא יהא מוכיחו וחוזר ומוכיחו ושלא יהא חברו רואהו ומתבייש ממנו, ושלא יהא בלבו עליו, ושאל יהיו המוכיחין מתוכחין, שהתוכחה מביאה לידי שלום. וכן אתה מוצא באברהם והוכיח אברהם את אבימלך. וכה"א ביצחק מדוע באתם אלי. וכן אתה מוצא ביעקב ויקרא יעקב אל בניו ראובן אומר לך מפני מה לא הוכחתיך כל השנים הללו כדי שלא תניחני ותדבק בעשו אחי. וכן אתה מוצא במשה ויהי בארבעים שנה בעשתי עשר חדש וגו'. וכן אתה מוצא בשמואל שנאמר הנני עני בי נגד ה' ונגד משיחו. וכן אתה מוצא בדוד שלא הוכיח את שלמה אלא סמוך למיתה שנאמר ויקרבו ימי דוד למות ויצו את שלמה בנו לאמר וגו':