Document User Data in new document

Hi everyone.
How to write and read Document User Data on documents is explained at the link https://developer.rhino3d.com/guides/rhinocommon/plugin-user-data/, but if I create a new document, Document User Data of the previous remain in memory.
On which event or command can I reset Document User Data when I create a new document?

Take a look at the static events that are available on the RhinoDoc class https://developer.rhino3d.com/api/RhinoCommon/html/Events_T_Rhino_RhinoDoc.htm

Probably you need the NewDocument event. If you’re using/supporting Mac Rhino, the user can have multiple documents open. Then you also need to monitor ActiveDocumentChanged.

I try this, but don’t work.

RhinoDoc.NewDocument += (s, e) => { PluginData.CodeDlgObjClass = null; };

The value of CodeDlgObjClass don’t set to null on open new document.

UPDATE: If I create a new file without template, the event is called, but if I create a new document from template it isn’t called.
I think, in this case, it’s a load file, not a new file command, right?

You’ll also want to handle RhinoDoc.OpenDocument.

– Dale