How do I delete an OCG/layer completely from PDF?

Q: i want to delete a OCG (layer). But if I do that, the elements from
ocg gets merged with the default content.

So, how do I delete a OCG completely?
A: The problem is that in PDF layers are always part of PDF content.
Actually layers are implemented as marked content (tags) which
associate parts of PDF content stream with a dictionary describing the
layer (i.e. OCG dictionary). It seems that what you are looking for is
a way to remove any parts of PDF page which are associated with a
given OCG group.

For example of how to remove specific elements from a PDF page please
see ElementEdit sample (
samplecode.html#ElementEdit). The only remaining issue is:

how to decide whether an element belongs to a specific layer?

The Element interface offer the following relevant methods:

  int GetStructMCID();
  bool IsOCVisible();

GetStructMCID should allow you to find the OCG for the given element
(if any) from 'Properties' dictionary stored in page.GetResourcesDict
(). Even simpler approach would be to configure the layer visibility
using high-level PDFNet API-s and then use element.IsOCVisible().