HOW TO

Automate deleting of objects owned by a schema

Last Published: April 25, 2020

Summary

GIS managers and administrators sometimes need to delete all feature datasets, feature classes and tables from a database owned by a particular schema.

Procedure

The below Python script provides an automated way of deleting feature datasets, feature classes and tables from a database owned by a particular schema.

  1. Modify the variables below according to the environment. The variables are located below main statement:

    logWorkspace = "C:/Temp"
    workspace = "C:/connections/LOCATOR_CONN/DATAOWNER.sde"
    dLogName = "Log_dataset_del.txt"
    fLogName = "Log_fc_del.txt"
    tLogName = "Log_table_del.txt"
    schema = "sde.DATAOWNER"

    Note:
    schema=DATAOWNER is case-sensitive:
    • Oracle: DATAOWNER
    • SQL Server: db_name.DATAOWNER
    • PostgreSQL: db_name.DATAOWNER


  2. Code:

    import arcpy, os, string
    def DelDatasets(logWorkspace,dLogName,workspace,schema):
    logfile = open(os.path.join(logWorkspace, dLogName), 'w')
    arcpy.env.workspace = workspace
    datasetList = arcpy.ListDatasets()
    for dataset in datasetList:
    if dataset.startswith(schema):

    arcpy.Delete_management(dataset)
    print ("Deleted Feature dataset {0}".format(dataset))
    logfile.write("Deleted Feature dataset {0}".format(dataset))
    logfile.write("\n")
    logfile.close()
    del logfile, datasetList

    def DelFeatureClasses(logWorkspace,fLogName,workspace,schema):
    logfile = open(os.path.join(logWorkspace, fLogName), 'w')
    arcpy.env.workspace = workspace
    fcList = arcpy.ListFeatureClasses()
    for fc in fcList:
    if fc.startswith(schema):

    arcpy.Delete_management(fc)
    print ("Deleted feature class {0}".format(fc))
    logfile.write("Deleted feature class {0}".format(fc))
    logfile.write("\n")
    logfile.close()
    del logfile, fcList

    def DelTables(logWorkspace,tLogName,workspace,schema):
    logfile = open(os.path.join(logWorkspace, tLogName), 'w')
    arcpy.env.workspace = workspace
    tableList = arcpy.ListTables()
    for table in tableList:
    if table.startswith(schema):

    arcpy.Delete_management(table)
    print ("Deleted table {0}".format(table))
    logfile.write("Deleted table {0}".format(table))
    logfile.write("\n")
    logfile.close()
    del logfile, tableList



    if __name__ == "__main__":

    logWorkspace = "C:/Temp"
    workspace = "C:/connections/LOCATOR_CONN/DATAOWNER.sde"
    dLogName = "Log_dataset_del.txt"
    fLogName = "Log_fc_del.txt"
    tLogName = "Log_table_del.txt"
    schema = "sde.DATAOWNER"
    DelDatasets(logWorkspace,dLogName,workspace,schema)
    DelFeatureClasses(logWorkspace,fLogName,workspace,schema)
    DelTables(logWorkspace,tLogName,workspace,schema)


Article ID:000011747

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic