I have some QGIS projects that I've been using for long time, adding and removing layers, updating reference to deleted or moved files etc.

Opening the qgs file with a text editor I see that it contains numerous references to different files, and this references are not showed on the Layer tree pane.

I have three different cases: references to existing file, references to missing files, references to database connections. I got aware of this when I opened my project on another machine, and QGIS reported a missing database connection to vector tables not present in the layer tree view.

Manually deleting references from the qgs file solved the problem, but I'm managing huge projects and it's near to impossible to manually check the project consistency. I've tried the "Remove empty layers" plugin, but the ones I'm trying to remove are not empty layers.

Is there any way to 'purge' qgis file?

  • Did you check by storing relative paths in .qgs file? Its at Setting--> Project Properties-->save paths-->relative Cheers N
    – Naresh
    Sep 17, 2012 at 11:41
  • What about "Save as ..." to a new project file?
    – AndreJ
    Sep 17, 2012 at 15:18
  • 1
    That did not work! I'll post to QGIS user mailing list a GQIS project file containing a phantom layer, not present in Layers pane.
    – bradypus
    Sep 17, 2012 at 19:59
  • 1
    Could you post that qgs file somewhere we could get to it? I'm unable to duplicate your situation. Perhaps all you need is a simple parser.
    – ike
    Apr 19, 2013 at 23:32
  • 1
    @ike Please check here: lists.osgeo.org/pipermail/qgis-user/2012-September/019327.html. You'll find there the test project I sent to the mailing list. To reproduce it you should place a tif file in this relative path: ./Planimetrie/2012/plan01_2012.tif. Please read also the following messages for more details.
    – bradypus
    Apr 20, 2013 at 12:57

2 Answers 2


Note: This code is no longer compatible with QGIS 3, see the answer below with a link to the Layer Board plugin instead https://gis.stackexchange.com/a/218719/9839

The layers in question are in the layer registry but not in the legend.

The following python script removes all layers which are loaded, but not shown in the legend.

Removing the layers:

registryLayers = QgsMapLayerRegistry.instance().mapLayers().keys()
legendLayers = [ layer.id() for layer in iface.legendInterface().layers() ]
layersToRemove = set( registryLayers ) - set( legendLayers )
QgsMapLayerRegistry.instance().removeMapLayers( list( layersToRemove ) )

It should be run from the python console inside QGIS. After applying it you can verify the operation by checking that the layers are gone from File (Project in 2.0) => Project Properties => Identifiable layers. Changes are not saved automatically

By adding the following line at the end, changes will also be saved automatically.


Please make a Backup copy of your Project file (.qgs) before overwriting your project file after applying this script.

For QGIS 1.8 the following line is required to be run initially:

iface = qgis.utils.iface
  • Ok i am getting errors when i enter it in. Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'legendLayerNames' is not defined
    – Ger
    Apr 22, 2013 at 9:13
  • 1
    QGIS 1.8 requires another way of accessing iface. The answer is updated to reflect this. Apr 22, 2013 at 9:27
  • 1
    I think you have it here. Well it worked in my case. And as a result made QGIS Cloud work better also as it is able to upload the layers now. Excellent.
    – Ger
    Apr 22, 2013 at 10:35

In addition to many other useful functions, the plugin named "Layer Board" (available within the Plugin installer) has a built-in button to "Remove ghost layers" that solves this problem.

In addition to the 1-click button to remove ghost layers, it also allows you to view and remove individual layers in a nice GUI that lists each layer and its properties. You can select any of these individual layers from the table and click the "Remove Layer" button to resolve each issue individually, if you choose.

In my case, the ability to remove individual layer references was very important, because I had some "ghost layers" that had the same name but were referenced 2-3 times in the registry. I used the layer board to remove the correct entries individually and get back to the correct single layer reference.

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.