1

I'm running an automated process that outputs shapefiles. However, there are these types of files that are not useful, they are garbage, so it is necessary for me to delete them.

I know that in Python the following function deletes only one file:

import os
os.remove()

However, a shapefile is more than a single file (.shx, .dbf, .prj, .cpg).

Using ArcPy, is there a way to delete all the files of a "shapefile"?

1
  • 5
    arcpy.Delete_management is the tool of choice
    – Vince
    Feb 4, 2022 at 15:39

2 Answers 2

3

To elaborate slightly on @Vince's comment, use arcpy.Delete_management("C:/path/to/shapefile.shp") -- the files with other extensions will be automatically removed as well.

1
  • 6
    ...be sure that the shapefile(s) are not open in any os-based viewer, file explorer, or software which creates locks on the files, otherwise the arcpy management will raise errors as well as leave you with incomplete deletions. Feb 4, 2022 at 20:50
2

As already pointed out by others, you can use arcpy.management.Delete:

import arcpy

file = r"D:\projects\playground\python\stackgis\data\test.shp"

if arcpy.Exists(file) and arcpy.TestSchemaLock(file):
    arcpy.management.Delete(file)
else:
    print(f"Cannot delete {file}.")
    print("The file either doesn't exist or is locked by another application.")

JasonInVegas added a good comment to another answer: make sure that there is no lock on the shapefile through another application.

You can achieve this by using arcpy.TestSchemaLock. The function tests if a lock can be acquired and returns True if so. If there is already a lock on the data, no lock can be acquired and the function returns False.


Alternatively, you could use GDAL/OGR:

import os

from osgeo import ogr  # conda install -c conda-forge gdal

shapefile = r"D:\projects\playground\python\stackgis\data\test.shp"

driver = ogr.GetDriverByName("ESRI Shapefile")

if os.path.exists(shapefile):
    driver.DeleteDataSource(shapefile)

Or, you could write your own function to delete the shapefile along with the associated files:

import os
from pathlib import PurePath

shapefile_extensions = [
    "shp", "shx", "dbf", "sbn", "sbx", "fbn", 
    "fbx", "ain", "aih", "atx", "ixs", "mxs", 
    "prj", "xml", "cpg"
]

def delete_shapefile(shapefile):
    
    if os.path.exists(shapefile):

        path = PurePath(shapefile)
        os.remove(shapefile)

        for extension in shapefile_extensions:

            associated_file = f"{path.parent}\\{path.stem}.{extension}"
            if os.path.exists(associated_file):
                os.remove(associated_file)


shapefile = r"D:\projects\playground\python\stackgis\data\test.shp"

delete_shapefile(shapefile)

Something to consider: delete_shapefile deletes files based on the shapefile's name and the associated file extensions.

That can lead to an unwanted side effect: If someone names an unrelated XML file (or another file with a shapefile-associated extension) same as the shapefile, that file will be deleted as well.

However, the question would be, does ogr and/or arcpy do a deep check? I don't know.

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.