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).
Delete layers/files rather than remove – maskin Sep 12 '18 at 15:19