Delete Field (Data Management)

Summary

Deletes one or more fields from a table, feature class, feature layer, or raster dataset.

Usage

    Caution:

    This tool modifies the input data. See Tools that modify or update the input data for more information and strategies to avoid undesired data changes.

  • You can specify either the fields to delete or the fields to keep.

    • To delete fields, use the Field(s) parameter to specify the fields to delete, and set the Method parameter to the Delete Fields option.
    • To keep fields, use the Field(s) parameter to specify the fields to keep, and set the Method parameter to the Keep Fields option.

  • Fields cannot be deleted from nonnative, read-only data formats in ArcGIS, such as VPF and CAD datasets.

Parameters

LabelExplanationData Type
Input Table

The table containing the fields to be deleted. The existing input table will be modified.

Mosaic Layer; Raster Layer; Table View
Field(s)

The fields to be deleted or kept from the input table, as specified by the Method parameter. Only nonrequired fields can be deleted.

Field
Method
(Optional)

Specifies whether the fields specified by the Field(s) parameter will be deleted or kept.

  • Delete FieldsThe fields specified by the Field(s) parameter will be deleted. This is the default.
  • Keep FieldsThe fields specified by the Field(s) parameter will be kept; all other fields will be deleted.
  • Delete FieldsThe fields specified by the drop_field parameter will be deleted. This is the default.
  • Keep FieldsThe fields specified by the drop_field parameter will be kept; all other fields will be deleted.
String

Derived Output

LabelExplanationData Type
Update Input Table

The updated dataset.

Table View; Raster Layer; Mosaic Layer

arcpy.management.DeleteField(in_table, drop_field, {method})
NameExplanationData Type
in_table

The table containing the fields to be deleted. The existing input table will be modified.

Mosaic Layer; Raster Layer; Table View
drop_field
[drop_field,...]

The fields to be dropped or kept from the input table, as specified by the method parameter. Only nonrequired fields can be deleted.

Field
method
(Optional)

Specifies whether the fields specified by the drop_field parameter will be deleted or kept.

  • DELETE_FIELDSThe fields specified by the drop_field parameter will be deleted. This is the default.
  • KEEP_FIELDSThe fields specified by the drop_field parameter will be kept; all other fields will be deleted.
String

Derived Output

NameExplanationData Type
out_table

The updated dataset.

Table View; Raster Layer; Mosaic Layer

Code sample

DeleteField example 1 (Python window)

The following Python window script demonstrates how to use the DeleteField function to delete specified fields.

import arcpy
arcpy.env.workspace = "C:/data"
arcpy.CopyFeatures_management("majorrds.shp", "C:/output/majorrds_copy.shp")
arcpy.DeleteField_management("C:/output/majorrds_copy.shp", 
                             ["STREET_NAM", "LABEL", "CLASS"])
DeleteField example 2 (Python window)

The following Python window script demonstrates how to use the DeleteField function to keep only the specified fields.

import arcpy
arcpy.env.workspace = "C:/data"
arcpy.CopyFeatures_management("majorrds.shp", "C:/output/majorrds_copy.shp")

arcpy.DeleteField_management("C:/output/majorrds_copy.shp", 
                             ["STREET_ALIAS", "DISTRICT_ID"], "KEEP_FIELDS")
DeleteField example 3 (stand-alone script)

The following stand-alone script demonstrates how to use the DeleteField function.

# Name: DeleteField_Example3.py
# Description: Keep several fields from a feature class and delete all the rest of the fields
  
# Import system modules
import arcpy
 
# Set environment settings
arcpy.env.workspace = "C:/data"
 
# Set local variables
inFeatures = "accident.dbf"
outFeatureClass = "C:/output/new_accident.dbf"
fields = ["STREET_NAM", "LABEL", "CLASS"]
method = "KEEP_FIELDS"
 
# Execute CopyFeatures to make a new copy of the feature class
#  Use CopyRows if you have a table
arcpy.CopyFeatures_management(inFeatures, outFeatureClass)
 
# Execute DeleteField
arcpy.DeleteField_management(outFeatureClass, fields, method)
DeleteField example 4 (stand-alone script)

Use the DeleteField function to delete all unnecessary fields from a feature class or table.

# Description: Delete unnecessary fields from a feature class or table.
 
# Import system modules
import arcpy
 
# Get user-supplied input and output arguments
inTable = arcpy.GetParameterAsText(0)
updatedTable = arcpy.GetParameterAsText(1)

# Describe the input (need to test the dataset and data types)
desc = arcpy.Describe(inTable)

# Make a copy of the input (so you can maintain the original as is)
if desc.datasetType == "FeatureClass":
    arcpy.CopyFeatures_management(inTable, updatedTable)
else:
    arcpy.CopyRows_management(inTable, updatedTable)

# Use ListFields to get a list of field objects
fieldObjList = arcpy.ListFields(updatedTable)

# Create an empty list that will be populated with field names        
fieldNameList = []

# For each field in the object list, add the field name to the
# name list. Exclude required fields to prevent errors
for field in fieldObjList:
    if not field.required:
        fieldNameList.append(field.name)

# dBASE tables require a field other than an OID and Shape. If this is
# the case, retain an extra field (the first one in the original list)
if desc.dataType in ["ShapeFile", "DbaseTable"]:
    fieldNameList = fieldNameList[1:]

# Execute DeleteField to delete all fields in the field list. 
arcpy.DeleteField_management(updatedTable, fieldNameList)

Environments

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes

Related topics