The faster way to check for and delete data

In repeated automated tasks, I often have to delete a set of data I am about to recreate fresh.

ArcPy offers a delete function, but the faster way to delete is through the os module.

In this example, I have a folder of shapefiles that I overwrite every time I run the script.   The first step in my script is to make a list of existing shapefiles in the folder and then step through the list deleting them.  Ideally we would just use the arcpy.env.overwrite = True setting, however this does not work with the FCtoShapefile command. Using the ArcPy tools, this deleting process can be pretty slow. Using the os tools, we speed things up significantly.
There are a few checks that help keep errors at bay – the first being a check for the existence of your data folder.  Since the list I generate is of every file in the folder, the second check is to make sure there are not folders in my list of things to delete.  I only want to delete pieces of shapefiles.  Since this folder is only used for shapefile output I’m taking the chance that those are the only types of files in here.  Otherwise I’d be more careful to check for the .shp, .shx. .dbf file extensions.
In the event the shapefile folder has been deleted entirely, the last error check is to create the folder if it doesn’t exist at all.
Here’s the sample
import os

outSHPS= "c:\\my_path\\to_the\\shapefiles"
print 'removing old shapefiles'
if os.path.isdir(outSHPS):
    files = os.listdir(outSHPS)
    for f in files:
      full_path = os.path.join(outSHPS, f)
      if not os.path.isdir(full_path):
  print 'Shapefile output directory does not exist. Making directory'

Image credits:mujalifah


One thought on “The faster way to check for and delete data

  1. Thanks so much for this fast way to delete shapefiles. May I ask you what do I have to add to the code if only those shapefiles should get deleted which are created by geoprocessing steps. So those shapefiles which are originally given to the geoprocessing shouldn’t get deleted.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s