Select to view content in your preferred language

Deleting FGDB

3551
6
10-22-2015 02:01 AM
SaurabhGupta5
Frequent Contributor

G'Day Everyone,

I am facing issues on deleting fgdb, scenario is like this :

" i have created fgdb programatically and loaded to TOC of ArcMap, after completion of work i want TOC to be clear and fgdb to be deleted but it is throwing an error"

The reason behind error is that fgdb is not released since arcmap is not closed. how to overcome this issue?

i am using following code to delete , please suggest

if (IO.Directory.Exists(iPLANConstants.strLSFGDBKeyPath + @"\" + clsLandSafeguarding.strDraftNameGlobal + ".gdb"))

                {

                    ArcMap.Document.FocusMap.ClearLayers();

                    ArcMap.Document.ActiveView.ContentsChanged();

                    ArcMap.Document.ActiveView.Refresh();

          string toolboxLoc = iPLANConstants.Instance.appconfig["dmtbx"].ToString();

            try

            {

                Geoprocessor gpProcess = new Geoprocessor();

                IGeoProcessor2 gp = new GeoProcessorClass();

                IVariantArray parameters = new VarArray();

                parameters.Add(iPLANConstants.strLSFGDBKeyPath + @"\" + clsLandSafeguarding.strDraftNameGlobal + ".gdb");

                try

                {

                   

                        gp.AddToolbox(toolboxLoc); // toolboxLox is correct

                        gp.Execute("Delete_management", parameters, null);

                        gp.RemoveToolbox(toolboxLoc);

                        log.Info(gp.GetMessage(0));

                        gp.ClearMessages();

                        gp = null;

                  

                }

                catch (Exception gpEx)

                {

                   

                }

Also tried below options but no success in deleting but no error also

string[] lockfiles = Directory.GetFiles(FileGDBLoc, "*.lock");

                        if (lockfiles.Length > 0)

                        {

                            //OPTION1

                            //ESRI.ArcGIS.DataManagementTools.Compact compactProcess = new ESRI.ArcGIS.DataManagementTools.Compact();

                            //compactProcess.in_workspace = FileGDBLoc;

                            //gpProcess.Execute(compactProcess as IGPProcess, null);

                            //OPTION2

                            //FileGDBWorkspaceFactory pWorkspaceFactory = default(FileGDBWorkspaceFactory);

                            //IWorkspace pWorkspace = default(IWorkspace);

                            //pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory();

                            //pWorkspace = pWorkspaceFactory.OpenFromFile(FileGDBLoc, 0);

                            //IDatabaseCompact databaseCompact;

                            //if (typeof(ESRI.ArcGIS.Geodatabase.IDatabaseCompact) == pWorkspace)

                            //{

                            //    databaseCompact = (IDatabaseCompact)pWorkspace;

                            //    if (databaseCompact.CanCompact())

                            //    {

                            //        databaseCompact.Compact();

                            //    }

                            //}

                            //else

                            //{

                            //    Console.WriteLine("The workspace does not support IDatabaseCompact");

                            //}

                        }

0 Kudos
6 Replies
DuncanHornby
MVP Notable Contributor

What relevance is the second set of code, it's compacting a geodatabase, not deleting it?

Even if you have cleared the TOC may be you have global objects that were referencing a layer, feature, workspace, row or a cursor? Search for these and set them to Nothing.

0 Kudos
SaurabhGupta5
Frequent Contributor

Hi Duncan,

Thanks for prompt reply!!

Second set of options i have tried because i read in other threads that compacting fgdb will clear lock files and help in deleting fgdb but in actual it is too not happening so i completely lost.

As per your suggestions i am clearing all objects as far it is possible but still no help.

I feel until arcmap is not closed and open it is not clearing link with fgdb. Please suggest any other way out.

Regards

Saurabh

0 Kudos
SaurabhGupta5
Frequent Contributor

Please help me out in this one. i am clearing workspace too but its not working.

0 Kudos
SaurabhGupta5
Frequent Contributor

After compressing the fgdb , i am able to delete all lock files from fgdb folder but not only this one i.e.

timestamps

Thus, i am still unable to delete fgdb programatically. This is very weird now and it seems like some issue in arcgis itself.

Regards

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Scratch ALL of what I mentioned before....see my new comments

Older thread....  erased all my previous advice...

William Craft 's comment helped me find who was accessing the "timestamps" file  https://community.esri.com/thread/53359#comment-265240 which was giving me a generic "can't do that" error.

Just in case the link ever breaks, I've quoted the comment below.   All I had to do is step 1 and I figured out who/the-machine that was locking my fgdb and there for giving me a generic "can't copy/delete/rename" error.

If you are still having issues with deleting a geodatabase on the file system despite the fact that the .lock files no longer exist, it's possible there is still a process lock on one or more of the files within it.  That being said, if you're using Windows Server 2008 / Windows 7 or later you can try one of the following methods:

1. From the Start menu, type FSMGMT.MSC, then multi-select the files you want from the GUI, then right click them and chose "Close".  That method should force close the files that are technically still open due to a process lock.  or
2. From a batch file, run the following (example is for a file geodatabase) to close a file named a00000225.gdbtable:
cd C:\this_server\directory\subdirectory
for /f "skip=4 tokens=1,2*" %%a in ('net files') do if %%b == C:\this_server\...\a00000225.gdbtable net file %%a /close

You can modify the command above to loop through all of the files in the file geodatabase to close them all rather than specify them individually, which would be tedious since there are so many. 

From the command prompt, type NET FILES to see what the underlined text above should contain. 

Remember that double percent characters are required for batch files (in other words, %% rather than %) but single percent characters are used when running the command outside of a batch script.

Another good thread is Clearing Arc/Arcpy workspace locks   Which has a script that I forgot about (sigh).

0 Kudos
DuncanHornby
MVP Notable Contributor

What goes around come around, in this case 5 years later! So I have ended up at this Q&A with similar issues, I am unable to delete a file geodatabase through ArcObjects.  I have a partial solution, using the geoprocessor I call the delete_management tool and systematically delete all the featureclasses then delete the geodatabase, this seems to work with the one exception that it leaves an empty folder with the name of the geodatabase. Not perfect but I can live with that.

Just thought I would share the experience.

0 Kudos