I developed a plugin in QGIS. One of the processes consists in remove some layers from the QGisProject, use them with other third party code in C# (maybe layers are modified by C# code) and finally open again these layers in QGIS. With QGIS 2.18 it works perfectly, but with version 3, when I try to do that, the C# code can't write on removed layers because DBF files are in use by another application (QGIS, I suppose).

I use this code to open layers:

try: #QGis 3.x
       QgsProject.instance().addMapLayer(vlayer, group is None)
except: #QGis 2.x
       QgsMapLayerRegistry.instance().addMapLayer(vlayer, group is None)

Then, to remove layers I use this one:

try: #QGis 3.x
except: #QGis 2.x

I think the problem is not in these lines, because some times the process works fine. Other times it works with some layers but not with another layers... Depending of the time that I wait to run the process after open the layers.

If I manually delete the layers from the QGIS user interface and run my process, it works fine.

I have the option of saving the project in a temporal qgis project file. Clear the QGISProject and finally (after C# code) open this temporal file... but it is a dirty solution when I only need to remove one layer...

I don't know what is happening. Some idea? It's possible to free memory of the QGIS project of these removed layers?

  • I am having the same problem, I delete a layer, but when trying to overwrite the file it is still blocked by qgis, you could share your code. thanks Mar 6, 2020 at 9:33

1 Answer 1


I noticed that if I clicked in a button to remove the layers and then I clicked in other button to make the rest of the process, it worked.

I tried to do some different things. At the end, I used QTask. I created a task to remove the layers and in the "on_finished" argument, I call to the second procces.

I think it is not very elegant, but it works.

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.