I my plugin I use the runalg function:

processing.runalg("qgis:clip",filepath + "/temp2.shp", Layer, filepath + '/' + layerName)
vlayer = QgsVectorLayer(filepath + '/' + layerName + '.shp',layerName, "ogr")

Afterwards I want to delete temp2.shp with

os.remove(filepath + "/temp2.shp") 

but I get the error WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:/Users/toke.nielsen/Desktop/temp/temp2.shp'

Is there a way to unlock temp2.shp after using runalg? And is there a way to clear all variables and memory when the plugin is done?

I have tried this, but it is not working with QGIS 2.2:

Releasing PyQGIS file locks?

  • I could avoid this, if I somehow could get the path of where my plugin is installed. Then I could make a temp folder which is deleted every time I use the plugin. But is there any command to get the installed path of my own plugin? The path could change from user to user.
    – Toke
    Commented Apr 15, 2014 at 11:16

3 Answers 3


The solution:




I was stuck here for some time as well until I figured it out. Just to clarify the layer that you want to remove you have to add to the canvas and then remove it. For anyone with same problem look on the answer here, and the rows with "#!!!!"..


Possibly it is a Python Garbage problem. If that is the case you should be able to remove the file after running this:

# If the name of the problematic layer is 'layer_name'
import gc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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