Select to view content in your preferred language

Delete Specific Feature Class

1766
10
Jump to solution
03-15-2022 03:35 PM
jaykapalczynski
Frequent Contributor

I am trying to read my SDE database and then delete specific feature classes if they exist...

I keep getting error below....not sure whats up....do I need to make a different connection to the db?

 

ExecuteError: ERROR 000601: Cannot delete C:/GIS_Scripts/Update/OS@Connection.sde/database.DBO.FEATURES_update. May be locked by another application.
Failed to execute (Delete).

 

def main():
    sqlPath  = r"C:/GIS_Scripts/Update/OS@Connection.sde/"
    fc_Delete = ["database.DBO.FEATURES_update","fc_Out2a"]

    for fc in fc_Delete:
             
      fc_path = os.path.join(sqlPath, fc)
      print fc_path
      
      if arcpy.Exists(fc_path):
        arcpy.Delete_management(fc_path)

 

0 Kudos
1 Solution

Accepted Solutions
jaykapalczynski
Frequent Contributor

Thanks Ya'll for your comments....turns out there was an issue with the FC itself.  I rebuilt the db and everything works regarding the DELETE....thanks again

View solution in original post

0 Kudos
10 Replies
DonMorrison1
Occasional Contributor III

I've never found a good way to get around this from python. Usually when my script fails (and I know nobody else should be using the database) I delete the locks on the database and then rerun the script. There is probably a less disruptive way - I'd be curious to find out. 

DonMorrison1_0-1647386291979.png

 

0 Kudos
RhettZufelt
MVP Notable Contributor

Do either of these feature classes exist:

C:/GIS_Scripts/Update/OS@Connection.sde/database.DBO.FEATURES_update
C:/GIS_Scripts/Update/OS@Connection.sde/fc_Out2a

as that is what is being told to delete (in that order).

Otherwise, normally when data is locked, it is by another user OR a service that is consuming the feature class.

You need to make sure you stop any services that are using that feature class.  If that isn't it, you can try to disconnect users from the database, and, maybe even block incoming connections while you do the delete.

R_

0 Kudos
jaykapalczynski
Frequent Contributor

The dbo.features_update FC exists....

Thats the crazy thing there are no locks....

That is ALL the code that I am trying to use....is there something else that I am missing....

jaykapalczynski_0-1647433819394.png

jaykapalczynski_1-1647433838856.png

 

 

0 Kudos
jaykapalczynski
Frequent Contributor

It almost appears that its creating its own lock when its trying to read if it exists....

 

0 Kudos
DonMorrison1
Occasional Contributor III

I would try to delete the table using the Delete Geoprocessing tool in ArcGIS Pro to see if you can establish that  it is related to something in your python code.  Another possibility is something in the database itself has it locked. Our systems use MS SQL Server and I would guess there are things you could do in the MS SQL Server Manager that would lock up a table. I've never had a problem with the arcpy.Exists call leaving the object locked. 

0 Kudos
jaykapalczynski
Frequent Contributor

OK so its telling me its locked but I have already cleared the locks....there is no service hitting this db and its on a test box so nothing should be accessing it....

  • I first check to make sure it exists
  • I then try to disconnect all users
  • I then test that specific FC for a lock

It keeps telling me its locked but when I check the DB and FC after it runs there are no locks?

    for fc in fc_Delete:
             
      fc_path = os.path.join(workspaceLocation, fc)
      print fc_path
      if arcpy.Exists(fc_path):
          arcpy.DisconnectUser(r"C:/GIS_Scripts/Update/OSConnection.sde/", "ALL")
          print fc_path + " exists"
          if not arcpy.TestSchemaLock(fc_path):
            print "Can't proceed - " + fc_path + " feature class is locked"
          else:
            arcpy.ClearWorkspaceCache_management()
            arcpy.Delete_management(fc_path)

 

0 Kudos
by Anonymous User
Not applicable

My first thought is are you using the connection for the data owner? Most of the time it will be the sde account. Not sure what your OS connection has available in terms of roles, but that is something to check as well.

0 Kudos
jaykapalczynski
Frequent Contributor

Thanks will check that out....

0 Kudos
jaykapalczynski
Frequent Contributor

Thanks Ya'll for your comments....turns out there was an issue with the FC itself.  I rebuilt the db and everything works regarding the DELETE....thanks again

0 Kudos