I have created many tables using Python and want to delete all tables with similar names. For example, I need to delete all tables ending with "level1", "level2" and until "level7". Also, I need to delete all tables ending with "_Merge". Here is the screenshot of my table list.

enter image description here

I write scripts using "ListTables" and then use for loop to delete them one by one. Here is my code:

levelfiles = arcpy.ListTables("level*")

for levelfile in levelfiles:

mergefiles = arcpy.listTables("_Merge")

for mergefile in mergefiles:


But this does not work. Could anyone offer some suggestions about deleting these files?

  • 1
    Before ever using Delete_management() you should always validate the parameter contents with a print or arcpy.AddMessage. If you did this, you would see that your wildcards aren't matching. – Vince Apr 10 '19 at 1:58
  • @Vince, thanks for your reply. Do you know how to refer to files including the same words? As the question indicated, how to refer to all tables ending with level1, level2 and so on? – Zhenyu Apr 10 '19 at 2:55
  • Since you've tried "level*", did you try "level"? Personally, I hate double-widcards and would use an if in the for. – Vince Apr 10 '19 at 3:26
  • Try "*level*" and "*_Merge". – Son of a Beach Apr 10 '19 at 5:23
  • Yeah, like this ^^^, without the SE reformatting issue. – Vince Apr 10 '19 at 13:33

Try "*level*" and "*_Merge" for your wildcard parameter values.

Alternatively, don't include a wildcard at all, but use an if, something like:

for theTable in arcpy.ListTables():
    if theTable[-6:-1] == "level" or theTable[-6:] == "_Merge":
        print "Deleting:  {}".format(theTable)

I would comment out that arcpy.Delete_management() line initially, and see what it says it's going to delete. Once the output looks right, uncomment the line and run it again.

This is a little safer as it's more precise about where in the table name the 'level' should appear.

| improve this answer | |
  • thanks for your help and this is what I need. Could you explain the meaning of "theTable[-6:-1]"? Maybe I can use your codes for another case. @Son of a Beach – Zhenyu Apr 10 '19 at 15:14
  • 1
    The square bracket notation is the standard python mechanism for extracting one element (no ':') or a 'slice' of multiple elements (with ':') from an interable object such as a list, or in this case a string. Think of a string as being a list of characters. The first character is at index '0'. The last character is at index '-1'. (You can count the characters either from the start, or using '-' (minus) to count backwards from the end). Full explanation at: pythoncentral.io/cutting-and-slicing-strings-in-python . [-6:-1] means the 6th last char to before the last char. – Son of a Beach Apr 10 '19 at 22:55

First little issue I see is that your second arcpy.ListTables is not capitalized right (you have arcpy.listTables). Python is case-sensitive, so this is important.

Secondly, arcpy.ListTables only searches in the current workspace. So before you search for the files, you need to tell it where to look by adjusting the workspace variable to the location where your tables are:

arcpy.env.workspace = r'c:\path\to\tables'

On a related note, your version of ArcMap (and Python) are very outdated - you should consider updating to something more recent and still supported if possible.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.