Is it possible to delete a field in a shapefile using GDAL with python?

With gdal 2.1.3 (Ubuntu 16.04), I can only see a CreateField() method on the layer object. No similar functions to delete a field.

Searching around, I can only find a ticket discussing the possibility and related rfc35.

Is this function implemented and how to use it?

4 Answers 4


You can also use OGR SQL ALTER TABLE statement to DROP the column directly via ExecuteSQL():

from osgeo import gdal

ds = gdal.OpenEx("my_shp.shp", gdal.OF_VECTOR | gdal.OF_UPDATE)
ds.ExecuteSQL("ALTER TABLE my_shp DROP COLUMN my_field")

I usually see this done by creating a copy of the shapefile, but specifying which fields to include in the copy. Once a copy is made, it's easy enough to delete the original file and rename the copy.

Here is a simple non-python way. Otherwise, I think you should be able to create a solution based on this python example.


To delete a column from the attribute table of a shapefile, I used this:

dataSource = driver.Open("MyShapefile.shp", 1) 
layer = dataSource.GetLayer()

where 4 is the index of the column I want to delete (starting from 0).

I hope it was what you asked.

  • We've just run into this problem. Calling DeleteField removes the field from the layer schema, but not from the features within the layer. The docstring says "This function should not be called while there are feature objects in existence that were obtained or created with the previous layer definition."
    – jon_two
    Commented Sep 14, 2018 at 11:35

If you need to know the field index of the field u want to delete, do like this:

lyr_definition = lyr.GetLayerDefn()
for n in range(lyr_definition.GetFieldCount()):
    field = lyr_definition.GetFieldDefn(n)
    field_name = field.name
    field_index = n ### After the loop u can type: lyr.DeleteField(field_index)
  • looping through a range can lead to calling a 'none' field in your list of fields if you delete them as you go. I tried to apply the above to a shapefile where i wanted to retain just 3 fields out of about 30. Instead use a while loop, where n <= number of fields. Update n and your number of fields each time you delete a field.
    – SimonDeS
    Commented Jan 5, 2021 at 14:18
  • U are absolutely right, this will work exclusively if u need delete only one field. Commented Jan 16, 2021 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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