I am having difficulty figuring out how to remove .sr lock files from shapefiles using ArcObjects and C# after removing the associated layer from the map. I notice that using the straight UI in ArcMap that removing a layer will remove the lock file, but I cannot get this to happen programmatically. I have tried everything that I can think of, including calling Marshal.FinalReleaseComObject on all variables. The following is some paired down test code that simply loads a layer into the map and then immediately removes it. This should remove the lock files, but it does not. Am I missing something?

// Loop through the selected shapefiles and load into the map

while ((pGxDataset = (IGxDataset)pGxObjects.Next()) != null)
    IFeatureLayer pFeatLayer = new FeatureLayer();
    pFeatLayer.FeatureClass = (IFeatureClass)pGxDataset.Dataset;
    pFeatLayer.Name = pFeatLayer.FeatureClass.AliasName;

    // Do something with the layer



1 Answer 1


This is an issue I've never solved in a satisfactory way. The only way in ArcMap I can reliably remove locks is to collapse all Catalog folders and right click > refresh them AND to open a new map in ArcMap. However, in code there's an ugly work around.

Each shapefile is several files all with the same name and a different extension. So when using a shapefile you can first copy all the files, bar the .sr.lock file, to a new location or to the same location with a different name, then work with that shapefile.

ArcMap does not lock shapefile's files. That means if you want to delete or replace the shapefile you can delete or overwrite all the shapefile's files with the exception of the lock files. The lock files should be deleted when ArcMap closes.

So putting it together you can copy the shapefile, do whatever you need to it, and then replace the original, and then refresh the ArcMap layer.

  • Danielm, thank you for the response. Unfortunately, I don't believe this will be a solution for me, because I am potentially opening 1000+ shapefiles and ArcMap is retaining lock files on all of the shapefiles. Eventually this causes ArcMap to choke and it bombs out. I am trying to prevent this from happening. That is why I need to do away with the lock files. As an fyi, I am cycling through folders and temporarily loading in the shapefiles so that I can work with them and then I remove them from the map and create a new map document for the next folder.
    – Weequay
    Mar 18, 2021 at 16:56
  • I guess my questions would be, do you need to load and unload into the table of contents or can you work with these shapefiles directly? Does this need to be done within ArcMap? In my experience lock files can be a little more cooperative if not loaded into the map. Also if this can be done outside of ArcMap, ending the thread the feature classes are opened in will release locks as well, so you can use multiple threads to accomplish your tasks. Additionally, if outside of ArcMap the number of locks may simply not be an issue.
    – danielm
    Mar 19, 2021 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.