Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Deep deleting everything in the layout as well as the Layout itself. (Python)

Hi Matthias,

I would like to learn the proper way of deep deleting everything inside the all layouts (pcells, static cells etc.) as well as the layouts itself. So I would like to create a condition just the same as when I first open the KLayout Application. Can you help me about how to do it programmatically?

Thank you!

Best Regards,


  • Hi Ege,

    you mean from a script?

    Did you create the layout objects yourself or do those live inside the main window?

    Closing all views should basically release all layout objects. That's halfway true for libraries, but I don't assume that's your problem.

    A problem with objects you created yourself (e.g. RBA::Layout::new) is that cleanup does not necessarily happen when you expect it to. This is in particular true for Ruby which has a garbage collection scheme. It is notoriously difficult to trigger when you need it. "GC.start" is a good way in Ruby, but not necessarily reliable in my experience.

    That's why the "big" objects have a method "_destroy" which you can call to explicitly release the object's resources. Note that also Region or Shape objects or arrays of such objects can become "big" and need to be released.

    Python in general is somewhat more reliable in terms of resource release as it uses reference counting mainly.

    But of all ways, the most efficient way it to shut down the process. In general, the object space of KLayout has some corners which are difficult to clean up. Libraries are one of these. The idea is to register libraries but not to unregister them anymore. This frees then system from the task of handling loss of library references at runtime.


  • With Ege We are using PCells created in Pyhton. Sometimes we change the PCell generation code and reload the python module. If KLayout has references to these PCells it crashes. To avoid crash we can use "Deep delete" in the Cell list in the GUI or turn the Cells static. This removes the references to the library and we can safely reload the python module. At the moment we do not know how to implement deep delete in python. Knowing this we can make a macro which does cleanup, reloads the library and regenerates the test cells.

    Perhaps you can advise how to achieve recursive "deep delete"? Our first attempt was to use layout_view.clear_layers(), but this likely keeps all the PCells in the cache.

  • Hi,

    I'm fairly sure that "pya.MainWindow.instance().close_all()" should basically remove all layouts and references.

    I'm a bit worried to hear you're getting crashes. Maybe it's worth debugging that. Are you able to reproduce one an tell me how?

    Kind regards,


Sign In or Register to comment.