How can one remove the saved username and password from ArcGIS layer (.lyr) files?
A scripted solution preferred but even a point and click method will be welcome.
Since 'even a point-and-click method is welcome' I am submitting the following which I use to clear out (or even re-assign) username/passwords from layerfiles and from layers within an MXD. This solution is based on ArcCatalog Version 10.2.2.3552.
Step 1: Open ArcCatalog, navigate to the folder containing the objective layer file, and right-click Properties to verify the current connection details saved in the layer file.
Step 2: Exit the Properties dialog, then add the layerfile to a new MXD and save the MXD somewhere convenient. I placed the MXD into the same folder path for now. Right-click on the MXD and choose 'Set Data Sources' (yes, I am borderline-ashamed to describe this sequence of steps because of how convoluted they are...but the steps work to reset the connection.)
Step 3: In the Set Data Sources dialog, choose "Replace All" as there is only one layer in the MXD. In the Replace All dialog, select and blank out the contents of the 'Replace with' line. then click the Replace All button. OK out of the Set Data Sources dialog and NOTICE that a new MXD has been created in the same location.
Step 4: Open the new MXD and immediately save the single layer as a new layer file.
Be sure to cancel out of any connection dialog boxes that appear as you open the new dialog <-- canceling out of these dialogs is important because your objective is to 'clear out' the connection, not re-assign it to a new set of credentials. (Notice in the image the red exclamation on the Table of Contents identifies a layer without connection credentials.)
Step 5: Return to Catalog, refresh the folder, then right-click on the newly-created layerfile to reveal its empty (cleared out) connection properties.
Again, it's not a perfect method by any means, but it does work to clear out the connection username and password from a layer file.
UPDATE: In order to get the connection unchanged, but to null out the username/password, perform this additional step BEFORE any replacement is made: Add a new database connection being sure to uncheck the save username/password box:
Then, in Step 3 during the Replace All action in the Set Data Source dialog, input the path of the newly-created connection so it can be used as the replacement:
I've used the script linked below with fairly good success. I'm able to select, and repoint multiple lyrfiles to a separate database connection. It seems to choke on Annotation, and Grouped lyrfiles, but the time this saves makes it worth using.
I'm able to re-source an lyrfile from a database connection containing user information, to one with a partial connection. Now when a user tries to use one of these lyrfiles, they are prompted for a password. Note: ArcGIS typically holds user information during a session, so it's a good idea to shut down all your windows, and reopen ArcGIS to properly test.
the solution to your question depends on the type of the layer. If the layer points to a geodataset in an sde connection file, Here is a quick solution. This script is doing the following tasks:
1- check the datasoure source to find the sde connection file
2- create a new connection file from previous one without the option to save username and password