Programatically remove Manager information from Portal Profiles

I've seen a few requests recently where folks want to get rid of the Manager/Direct Reports functionality within Portal. I suspect the most common reason for this that folks are making their sites externally available and dont want people outside of the company viewing the entire reporting structure.

Removing this functionality would entail removing the mapping from the AD attribute done in the profile properties configuration. Here's what a user profile would look like before this:

To remove the mapping you would view your profile properties and edit the manager field:

You then would change the mapping from the default of manager to Not mapped to Active Directory (which is at the top of the list).

After doing this, the properties of your user profiles will no longer show the linked icon as shown here:

However if unlike here, you already have your Profiles stamped with that information you would have to remove the manager information since it would no longer be in an active mapping. You could go through manually and clear the Manager field, but that would only be reasonable in a small implementation.

So to help those of you who are dealing with this--here's some code that can put it all together.

TopologyManager myTopologyManager =

new TopologyManager();
PortalSite myPortalSite = myTopologyManager.PortalSites[new Uri(textBox1.Text)];
PortalContext myPortalContext = PortalApplication.GetContext(myPortalSite);

UserProfileManager myUPM = new UserProfileManager(myPortalContext);
foreach(UserProfile myUP in myUPM)
{
myUP["Manager"] = null;
myUP.Commit();
}

You could look for a specific account\domain with an if statement looking for:
if (myUP["AccountName"] == "DOMAIN\\USERNAME")
or to restrict this operation to a specific domain:
if (myUP["AccountName"].ToString().ToUpper().StartsWith("DOMAIN"))

You may note while writing this code that myUP.DirectReports does exist as a DataSet. Know that it is a read-only computed value. DirectReports in this instance works much like how it does in AD. The Manager field is set to a value, DirectReports is simply a collection of users who have their Manager field set to the current User's account. As such, by clearing a user's Manager field--you are removing them from the Manager's DirectReports list as well.