6

In the Browser it's not possible to delete Shapefiles. In ArcMap you can delete Shapefiles easily within the Catalog.

How can I delete Shapefiles in QGIS without minimizing the QGIS-Window, navigating to the folder on my computer, select all the files that belong to a Shapefile (shp, dbf, shx, prj, etc.) and delete it?

  • Are you using QGIS Browser, or QGIS Desktop? – Richard Law Apr 20 '16 at 8:30
  • I use QGIS Desktop. – Enzo Baldini Apr 20 '16 at 8:34
  • 2
    Doesn't seem to be possible in the GUI, and IMO that's appropriate. – Richard Law Apr 20 '16 at 12:21
  • You can install ArcGis - in ArcGisCatalog select the file and press >delete< – frmante Oct 27 '16 at 1:27
  • Where can I download ArcGIS with ArcCatalog ready to install and use, @frmante? – Germán Carrillo Oct 27 '16 at 2:03
3

There is a built-in function for this in the QGIS API.

Select your layer in the Layers panel, then open the Python console and run the following:

from qgis.core import QgsVectorFileWriter

layer = iface.activeLayer() # If no layer selected, it will be empty and make the end of the script failed
shp_path = layer.dataProvider().dataSourceUri().split('|')[0]
print (QgsVectorFileWriter.deleteShapeFile(shp_path)) # Should return True if really deleted
2

You could also do this using the terminal and appropriate use of wildcards. For example, in bash running:

rm 'path/to/file'.*

will erase all the shapefile associated files for a particular file. It can be a bit dangerous though if you're not careful. I always try to be good and run the ls command (e.g., ls 'path/to/file'.*) to return a list of all files captured by the wildcard before removing them (for example, if you happen to have a .tif with the same file name that you don't want to erase as well).

That'd probably make a good feature request though!

  • Feature Request raised, though I figured the code from various answers would vary a little with the changes to QGIS 3. Qt5, PyQt 5, Python 3 ect... Delete layers/files rather than remove – maskin Sep 12 '18 at 15:16
2

Not sure if you can do this using the GUI but you can certainly do this using the Python Console. The following code goes into the specified path and deletes all files beginning with "File_Name":

import glob, os
path = 'path/to/folder//'

for fname in glob.glob(path + "File_Name.*"):
    os.remove(fname)

Edit:

Responding to the comment from @GermánCarrillo, you should enter the filename in full and then add a . at the end as shapefiles (in most cases atleast) end with different extensions after the .. So now there should be less chance of you deleting shapefiles which contain similar names.

  • 3
    That could be a bit dangerous. You might want to delete a file whose name turns out to be a prefix for other files, e.g., deleting "boundary" while having "boundary_country" in the same folder. – Germán Carrillo Apr 20 '16 at 20:53
  • 1
    @GermánCarrillo - Indeed it could be, I've edited the post to include a minor addition which should help a little to avoid deleting other files with similar names =) – Joseph Apr 21 '16 at 9:29
2

Building on Joseph's answer, you can delete any file in the Python console.

This is a little script that will delete all the files with a shapefile-associated extension in the directory of the 'foo.shp' file you give it:

import os, argparse

def deleteShapefile(aDir, aFile):

    fnameNoExt = os.path.splitext(aFile)[0]

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

    theFiles = [f for f in os.listdir(aDir) if os.path.isfile(os.path.join(aDir, f))] # get list of all files in that directory

    for f in theFiles:
        theFile = os.path.basename(f)
        name, extension = os.path.splitext()
        # If the name matches the input file and the extension is in that list, delete it:
        if (name == fnameNoExt or name == fnameNoExt + ".shp") and (extension in extensions): # handles the foo.shp.xml case too.
            os.remove f

def main():
    parser = argparse.ArgumentParser(description="A path to a file assumed to be a shapefile, this deletes all files in the same directory that have extensions possible in shapefile subfiles.")
    parser.add_argument('SHAPEFILENAME')
    args = parser.parse_args()
    realpath = os.path.realpath(args.SHAPEFILENAME)
    theDir, theFile = os.path.split(realpath)
    deleteShapefile(os.path.basename(realpath))


if __name__ == '__main__':
    main()

You could run that in a command terminal if you saved it as a .py file with something like

$ python deleteshapefile.py myshapefile.shp

If you find yourself doing it a lot, you could put that .py file somewhere on the path given in your environment variable PYTHON_PATH and just import it in the console inside QGIS, in which you just call deleteShapefile(aDir, aFile).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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