QGIS Shapefiles – Deleting Shapefiles with All Related Files

deleteqgisshapefile

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

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?

Best Answer

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