I have been using ArcGIS 10.3.1.
I have a list of tables in an enterprise geodatabase with date and time in the table name. Every day one table is created, as an outcome of some geoprocessing tasks with the date and time in the table’s name when the table is created.
Naming format of the tables are FHRT_YYYYmmdd_HHMMSS
Like
etc.
I want to create a python script that will keep the latest 3 tables (in this example FHRT_20190808_071015, FHRT_20190807_071218 and FHRT_20190806_071119) and delete the rest with similar name format. The geodatabase may have other tables with other name format (cadastre, property etc.), I do not want to touch those.
Being new in Python I do not know how I would create the script that will specify the dates.
I mean how will I instruct the script to keep the 3 tables that have the latest dates and delete the older tables?
What module do i need to import and what would be the statement?
Thank you in advance.
Regards,
Hafiz
Still the same error.
The code now looks like
import arcpy
Delete_succeeded = "true"
arcpy.env.workspace = "C:\\Users\\app_arcgis\\AppData\\Roaming\\ESRI\\Desktop10.3\\ArcCatalog\\VMAPPDBUAT_GISDEL_GIS.sde"
Tables = arcpy.ListTables("FHRT_20*")
old_one = sorted(Tables, reverse=False)[:1]
print ("\n")
for one in old_one:
print(one)
print("{} tables found\n{}".format(len(Tables), Tables))
arcpy.Delete_management(one)
and the error is
>>>
Traceback (most recent call last):
File "C:\FishHealth\Scripts\DeleteOldTable.py", line 13, in <module>
arcpy.Delete_management(one)
NameError: name 'one' is not defined
>>>
Although your code works for a file geodatabase saved locally.
Thank you for your help.
Looking at Line #02 of your results:
[]
That line shows that Tables is an empty list, i.e., there are no tables being returned from arcpy.ListTables. Since Tables is empty, sorted will also return an empty list, and your line "for one in old_one" will never be execute and create the variable "one".
Your issue is with listing tables, not deleting them.
It is happening only when the workspace is an sde database connection (C:\\Users\\app_arcgis\\AppData\\Roaming\\ESRI\\Desktop10.3\\ArcCatalog\\VMAPPDBUAT_GISDEL_GIS.sde)
The code lists tables when the workspace is a file geodatabase (\\\\scamain\\gis_data\\FHRITest\\FHRITest.gdb)
and deletes the table listed in line 6 of the following result, which is the oldest in the tables' list.
>>>
10 tables found
[u'FHRT_20190810_070048', u'FHRT_20190814_070034', u'FHRT_20190813_153517', u'FHRT_20190813_153907', u'FHRT_20190813_154351', u'FHRT_20190813_154438', u'FHRT_20190813_154801', u'FHRT_20190813_154945', u'FHRT_20190813_155547', u'FHRT_20190813_155829']
FHRT_20190810_070048
>>>
If you can't even get a list of tables from *.sde, then they either aren't there or there is something inherently wrong with the workspace and/or the connection.
Also, I noticed "Desktop 10.3' in the path. It might be prudent to upgrade to rule out searching for an old 'bug' that is now fixed
Managed to resolve.
instead of
Tables = arcpy.ListTables("FHRT_20*")
I should have used
Tables = arcpy.ListTables("GISTEST.TEST.FHRT_20*")
as the wild card. I missed the database and the user name.
Now it works.
Thank you Dan and Joshua for your kind effort to help me.