The verse states: "And there was famine in all the land of Egypt, and the people cried to Pharaoh for bread; and Pharaoh said to all Egypt: 'Go to Yosef; whatever he says to you, do'" (Bereshit 41:55).
Regarding the phrase "whatever he says to you, do," Rashi explains that Yosef told the Egyptians that, in exchange for provisions, he demanded that they circumcise themselves. When they went to complain to Pharaoh and said, "Thus he tells us to do," Pharaoh responded: "Why did you not store grain? Did he not announce to us that years of famine were coming?" They told him: "We stored much, but it rotted." He said to them: "If so, 'whatever he says to you, do,' for if he decreed upon the grain and it rotted, then what if he were to decree upon us that we die?"
A great difficulty arises here: How is it possible that Yosef forced them to circumcise themselves, given that even after circumcising, they remained Egyptians and that circumcision was not for the purpose of conversion to the People of Israel? For we have established in the Shulchan Aruch (Yoreh De'ah 263:5) that it is forbidden to circumcise a non-Jew unless it is with the intention of conversion. This is especially difficult according to what the Taz wrote there (Chapter 158, subsection 3): since it is known from the words of our Sages that Abraham Avinu sits at the entrance of Gehinnom (Hell) and does not allow any circumcised Jew to descend there, it turns out that one who circumcises a foreigner nullifies this distinguishing sign of Israel. According to this, the difficulty is even greater: How could Yosef force them to circumcise themselves?
It can be explained that Yosef knew that the entire matter of his sale had occurred because there was a reason stemming from HaKadosH BarucH Hu, so that they would descend to Egypt and be enslaved there. And the essence of the exile was because Israel annulled the fulfillment of the covenant of circumcision in Egypt, as explained in the Midrash (Shemot Rabbah, Ch. 1, Art. 8), that when Yosef died, they broke the covenant of circumcision, and because of that, the exile began.
According to this, those who were born and died in Egypt before the Exodus could not be distinguished by Abraham to save them from Gehinnom, since they were uncircumcised. Therefore, Yosef acted with wisdom and commanded that the Egyptians also be circumcised. His goal was to nullify this distinguishing sign for that generation, so that Abraham would not look at that generation based on the sign of circumcision, but rather would save from Gehinnom anyone who was of the descendants of Israel in general terms, or anyone who had suffered the exile in Egypt [and not necessarily only those who were circumcised].
Zera Shimshon, Parashat Vayigash, Art. 4