I have a shapefile that is being used as an input into the start of a model. The model is very simple but makes up a much larger model.

The input is reprojected in the first step but is not needed ever again. Is there a way I can simply delete that shapefile permanently from my computer within the modeler itself?

  • How is your input shapefile selected? Do you select it from the drop-down list (i.e. it is already loaded into QGIS) or do you select it using the File Dialog? – Joseph Oct 5 '17 at 12:40
  • Yes I select it from the drop-down list as it is already loaded into the QGIS project. – AWGIS Oct 5 '17 at 14:42
  • Then as far as I know there is no way to delete the file with your model. Tried that myself and could not find a working solution. – AndreasK Oct 5 '17 at 14:50

I think the only way to this currently is to write a custom script which removes the layer from the table of contents and then deletes the shapefile.

You can create one from:

Processing Toolbox > Scripts > Tools > Create new script

Then use something like the following:


from qgis.core import QgsMapLayerRegistry, QgsVectorFileWriter

layer = processing.getObject(Layer_to_delete)

Save the script into your /.qgis2/processing/scripts directory. Then add the custom script from the list of algorithms to your model, select, the input layer as the Layer to delete and set the Reproject layer tool as the parent algorithm (the script will run when the tool has finished):

Example model

When you run the model, once the Reproject layer tool has finished, the selected input layer should be removed from the interface and deleted (make sure to keep backups!)

|improve this answer|||||
  • I have tried running the model as described but when it gets to the script it crashes QGIS. I am not really sure why. – AWGIS Oct 5 '17 at 18:32
  • My coding is very poor so am I right or wrong in thinking that the script you described should work as it is as long as I have correctly linked the input layer and set the reproject layer tool as the parent algorithm? – AWGIS Oct 5 '17 at 18:41
  • I have noticed that when running the script on its own it doesn't work. However if I remove the line: QgsVectorFileWriter.deleteShapeFile(layer.source()) then it runs fine and removes the map layer. – AWGIS Oct 5 '17 at 19:01
  • @AGW94 - Which QGIS version and platform are you using? I used QGIS 2.18.2 for Win7 64-bit. Do you receive any error messages when QGIS crashes? – Joseph Oct 6 '17 at 9:12
  • 1
    @AGW94 - I just tested on 2.18.13 and the model and script above works fine. Does QGIS crash with the same shapefile? Could you create a simple one and test it? – Joseph Oct 6 '17 at 10:01

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.