[GIS] can not delete file after using runalg function

pyqgisqgis

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")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

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?

Best Answer

The solution:

QgsMapLayerRegistry.instance().removeMapLayer(vlayer.id())

before

os.remove()