I know this is years late, but I struggled with this as well and, for me, Kirk's answer working successfully depended on how the join was created. I don't have enough rep to comment on Kirk's answer, but it threw an exception until I changed it to what's below. Namely, I switched sourceTable and destinationTable. I also had to add the ComReleaser for the lock file to be removed from the standalone table, which in my case was a DBF.
private void RemoveAllJoins2(ESRI.ArcGIS.Carto.IFeatureLayer joinedLayer)
{
using (ESRI.ArcGIS.ADF.ComReleaser comReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
{
var dispTable = joinedLayer as ESRI.ArcGIS.Carto.IDisplayTable;
comReleaser.ManageLifetime(dispTable);
var rqt = dispTable.DisplayTable as ESRI.ArcGIS.Geodatabase.IRelQueryTable;
if (rqt != null)
{
comReleaser.ManageLifetime(rqt);
var sourceTable = rqt.SourceTable as ESRI.ArcGIS.Geodatabase.IDataset;
var destTable = rqt.DestinationTable as ESRI.ArcGIS.Geodatabase.IDataset;
comReleaser.ManageLifetime(destTable);
System.Diagnostics.Debug.Print("source: {0}", sourceTable.Name);
System.Diagnostics.Debug.Print("dest: {0}", destTable.Name);
joinedLayer.FeatureClass = (ESRI.ArcGIS.Geodatabase.IFeatureClass)sourceTable;
}
else
System.Diagnostics.Debug.Print("there are no joins");
}
}
Finally, the above method (RemoveAllJoins2) only worked for joins that were created with a RelQueryTable. It did not remove joins that were created in ArcMap with the Join tool in ArcToolbox, or by the join tool accessed via the Join and Relates menu item in the context menu of a layer. It also did not remove joins that were created with IDisplayRelationshipClass in ArcObjects. The sample below (AddJoins2) is the only type of join I could create that was removed with the RemoveAllJoins2. Additionally, I could not remove the join created by AddJoins2 via the Remove Join tool in ArcToolbox, or with the Join and Relates menu item, or by setting the IDisplayRelationshipClass.DisplayRelationshipClass to null.
private void AddJoins2(ESRI.ArcGIS.Carto.IFeatureLayer inputLayer, string inputJoinField, ITable joinTable, string outputJoinField)
{
// Build a memory relationship class.
Type memRelClassFactoryType = Type.GetTypeFromProgID("esriGeodatabase.MemoryRelationshipClassFactory");
IMemoryRelationshipClassFactory relationshipFactory = (IMemoryRelationshipClassFactory)Activator.CreateInstance(memRelClassFactoryType);
string joinName = inputLayer.Name + "_" + ((IDataset)joinTable).Name;
using (ESRI.ArcGIS.ADF.ComReleaser comReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
{
IRelationshipClass rClass = relationshipFactory.Open(joinName, inputLayer.FeatureClass, inputJoinField, (IObjectClass)joinTable, outputJoinField, "ForwardPath", "BackwardPath", esriRelCardinality.esriRelCardinalityOneToOne);
comReleaser.ManageLifetime(rClass);
// Open the RelQueryTable as a feature class.
Type rqtFactoryType = Type.GetTypeFromProgID("esriGeodatabase.RelQueryTableFactory");
IRelQueryTableFactory rqtFactory = (IRelQueryTableFactory)Activator.CreateInstance(rqtFactoryType);
IRelQueryTable relQueryTable = rqtFactory.Open(rClass, true, null, null, String.Empty, false, true);
// Set the feature layer's feature class to the RelQueryTable feature class
inputLayer.FeatureClass = (IFeatureClass)relQueryTable;
}
}