# How do I remove layers or copy a layer to a new file?

**URL:** <https://forum.mupdf.com/t/how-do-i-remove-layers-or-copy-a-layer-to-a-new-file/183>\
**Category:** PyMuPDF\
**Created:** [October 20, 2025, 9:03am UTC](https://forum.mupdf.com/t/how-do-i-remove-layers-or-copy-a-layer-to-a-new-file/183 "2025-10-20T09:03:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![p-vdp](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/p-vdp/32/254_2.png) [@p-vdp](https://forum.mupdf.com/u/p-vdp)\
**Post date:** [October 20, 2025, 9:03am UTC](https://forum.mupdf.com/t/how-do-i-remove-layers-or-copy-a-layer-to-a-new-file/183/1 "2025-10-20T09:03:29Z")

</div>

I’m new to this package, and I’ve figured out how to toggle layers on/off:

```python
import pymupdfd
doc = pymupdf.open("file.pdf")
ogcs = doc.get_ocgs()
lyr_xref = ogcs[0]
doc.set_layer(-1, on=[lyr_xref], basestate="OFF") # turn off all but the first layer

```

But how do I delete them, or copy a specific layer to a new file?

Also see discussion here: [How do I remove layers or copy a layer to a new file? · pymupdf/PyMuPDF · Discussion #4758 · GitHub](https://github.com/pymupdf/PyMuPDF/discussions/4758)

---

<div class="post-metadata">

**Author:** ![HaraldLieder](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/haraldlieder/32/14_2.png) [@HaraldLieder](https://forum.mupdf.com/u/HaraldLieder)\
**Post date:** [October 20, 2025, 10:14am UTC](https://forum.mupdf.com/t/how-do-i-remove-layers-or-copy-a-layer-to-a-new-file/183/2 "2025-10-20T10:14:16Z")

</div>

I also posted an answer in the repo’s Discussions, so just a few comments here:

What usually is called a “layer” actually refers to the PDF object type OCG - Optional Content Group (not the most fortunate naming in my mind).  
These are small objects - in the range of 20 - 30 bytes - that have a handful of attributes, of which one, the “name” is only required.  
An OCG is a boolean value really: it can be ON or OFF.

> The PDF specification also has the object type _ **OCMD** _ (Optional Content Membership Dictionary). This can define arbitrary boolean expressions of multiple OCGs - in principle supporting the full power of boolean algebra - including “AND”, “OR”, “NOT” and brackets.

Whether or not some PDF object is visible can be made dependent on the value of an OCG or an OCMD.  
This dependency is implemented by referring to the XREF of some OCG / OCMD.  
For this very reason, an OCG should **_never_** be physically deleted as a PDF object: this could make the PDF as a whole unusable.

However: A PDF takes note of an OCG if and only if the OCG’s XREF is present in the catalog’s array `/OCProperties/OCGs`. Removing an OCG entry from there is the only possible approximation of “deleting a layer”.

So the term “layer” in a PDF is in no way comparable with a layer of a cake: you cannot take it out and get a smaller cake … or reuse it to enrich another cake with a layer of strawberries 😆.

---

<div class="post-metadata">

**Author:** ![HaraldLieder](https://yyz2.discourse-cdn.com/flex004/user_avatar/forum.mupdf.com/haraldlieder/32/14_2.png) [@HaraldLieder](https://forum.mupdf.com/u/HaraldLieder)\
**Post date:** [October 20, 2025, 11:34am UTC](https://forum.mupdf.com/t/how-do-i-remove-layers-or-copy-a-layer-to-a-new-file/183/3 "2025-10-20T11:34:30Z")

</div>

The initial idea of our user @p-vdp was correct - and the only possible one:

If we need a new PDF that shows the page content related to only one of the many “layers” (= OCGs), then setting the wanted OCG to `ON` and all other OCGs to `OFF` **_permanently_** is the only viable way.  
Deleting / disabling unwanted OCGs does the opposite: PDF objects referring to them would now always be visible!
