I have a Python script that processes several country sized OpenStreetMap PBF files at a time. The problem I'm having is that: each time I open and process one of these files, I lose anywhere from 200MB to a gig of hard drive space (I'm assuming to some temp storage directory somewhere).

When I process loads of these files, I eventually end up running out of disk space and the script crashes. I've tried several ways to get the script to unload the QgsVectorLayer object that I'm processing in an attempt to reclaim some disk space. These include:

  1. Placing the loading & processing of the individual layers into a separate method that (I assumed) would be automatically removed once the method returns.
  2. Explicitly calling del on the layer object.
  3. Calling QgsProject.instance().removeMapLayer on the layer in the hope to get it removed some how.

Unfortunately, none of these worked, so far the only thing that seems to unload the layers and delete the temporary files is allowing the script to complete fully.

Is there a way for me to ensure that, once I'm done with a QgsVectorLayer, I can close it, and have it clean up its temp data?

1 Answer 1


It has been answered here and here.

You need to first add the layer to map even if you don't display it before removing it.

QgsProject.instance().addMapLayer(vlayer, False)

It usually works, but I recently had an issue with FLT files (raster format) which seems to stick whatever I try.

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.