3

I need to truncate file geodatabase tables (1 or all) with Python. What is the code for that?

3 Answers 3

10

As of 10.1, Esri has added Truncate Table (Data Management). It will remove all records from a table, regardless of a table view selection, and does not support versioned tables. It is much faster, though.

Esri's sample code for truncating all tables in a geodatabase:

# Set the workspace.
arcpy.env.workspace = "C:/work/vancouver.gdb"

# Get a list of all the tables.
tableList = arcpy.ListTables()

# Loop through the list and run truncate
for table in tableList:
    arcpy.TruncateTable_management(table)
7

AFAIK, you can use Delete Rows method in arcpy. from Arcgis Resource Center:

Delete Rows (Data Management)

Summary

Deletes all or the selected subset of rows from the input.

If the input rows are from a feature class or table, all rows will be deleted. If the input rows are from a layer or table view with no selection, all rows will be deleted.

consider this caution:

If run against a layer or table view that does not have a selection, the operation cannot be undone using undo/redo.

Example Code:

import arcpy
from arcpy import env

env.workspace = "C:/data"
arcpy.CopyRows_management("accident.dbf", "C:/output/accident2.dbf")
arcpy.DeleteRows_management("C:/output/accident2.dbf")
-2
import arcpy

from arcpy import env

wrksp = r"workspace"

env.workspace = wrksp


fcs = arcpy.ListFeatureClasses()

for fc in fcs:

    arcpy.MakeTableView_management(fc, "fc_Temp")

    arcpy.DeleteRows_management("fc_Temp")
1
  • If the goal is truncation, DeleteRows is the wrong tool. Instead, use TruncateTable. Note that code-only ("wall of code") questions get downvotes for failing to ask a Question; wall of code answers also fail to provide an Answer. Commented Oct 20, 2022 at 0:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.