Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 with a text editor the qgs file I see that it contains numerous references to files, and this references are not showed on the Layer tree pane.

I have here different cases: references to existing file, references to missing files, references to database connections. I got aware f 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?

share|improve this question
Did you check by storing relative paths in .qgs file? Its at Setting--> Project Properties-->save paths-->relative Cheers N – Naresh Sep 17 '12 at 11:41
What about "Save as ..." to a new project file? – AndreJ Sep 17 '12 at 15:18
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 '12 at 19:59
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 '13 at 23:32
@ike Please check here: 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 '13 at 12:57

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.

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

iface = qgis.utils.iface

Removing the layers:

registryLayers = QgsMapLayerRegistry.instance().mapLayers().keys()
legendLayers = [ 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.

Required Minimum QGIS Version: 1.8

share|improve this answer
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 '13 at 9:13
QGIS 1.8 requires another way of accessing iface. The answer is updated to reflect this. – Matthias Kuhn Apr 22 '13 at 9:27
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 '13 at 10:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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