title: How to remove CKeditor instances after the editor has been removed from the DOM
body: <p>There have been a few related questions on SO about how to remove CKeditor instances, most using <code>.destroy()</code>. This is fine when the editor is still in the dom, however. I have a situation where a CMS is adding a text area, which I am converting to a CKeditor. the cms moves elements from the dom (on an unrelated event).</p>
  <p>I can bind an event to the <code>complete</code> emit of that unrelated event, so that the CKeditor can be re-initialized. All that works fine but when I come to remove those left over instances I get errors, this is how I tried:</p>
  <pre><code>for (var name in CKEDITOR.instances) {
      CKEDITOR.instances[name].destroy(true);
  }
  </code></pre>
  <p>but it generates a error:</p>
  <blockquote>
  <p>Uncaught TypeError: Cannot read property 'clearCustomData' of null</p>
  </blockquote>
  <p>I have <a href="http://jsfiddle.net/pf2d491c/1/" rel="nofollow noreferrer">recreated the issue on a jsfiddle</a> You'll see that the code:</p>
  <ol>
  <li>creates a ckeditor instance from a textarea</li>
  <li>removes the parent dom element</li>
  <li>creates new dom elements</li>
  <li>tried to remove all current instances</li>
  <li>creates a new ckeditor instance</li>
  </ol>
  <p><em>Step 4 generates the error.</em></p>
  <p><em><strong>n.b.</strong></em> related questions on removing ckeditor instances relate to still existing dom elements where in this situation the dom has been removed. I have no control over the dom element being removed.</p>
  <p>Is there a way to bind an event to the ckeditor triggered when it parent dom elements are removed? I'd assume I'd still hit this issue even if that was the case. Any one any idea?</p>
  <p><em><strong>n.b.</strong></em> <em>I am using jQuery in other places in this project, so a solution involving jQuery would be suitable even though I'm not using it in the ckeditor code.</em></p>
  <p>UPDATE:</p>
  <p>I did try setting the instances to <code>null</code>, which does work, but doesn't kill the object created on a CKeditor instance. i.e. <code>CKEDITOR.htmlDataProcessor</code>
  <em>(using 'Profiles -&gt; Heap snapshot' in Chrome dev tools is a good demo of the issue)</em></p>
  <p>UPDATE:</p>
  <p>Thanks @oleq for the information, I looked at the discussion but couldn't so how to add to it so updated here:</p>
  <p>Please see the image of mem profiles before and after dom changes.</p>
  <p><img src="https://i.stack.imgur.com/odcqF.jpg" alt="dom changes effecting memory" /></p>
  <p>You'll see the size of the CKEDITOR properties really go up.</p>
  <p>I notice that <code>CKEDITOR.instances.editor1.element.$</code> stores the element the ckeditor has been bound to/fired upon. Would a check for an existing ckeditor instance on that element, which would destroy the  existing <code>CKEDITOR.htmlDataProcessor</code> etc objects and recreate before a new instance is added to the text area?</p>
  <p>Does that sound like something that would be appropriate or would the fact that this is seen as an edge case mean its too low a priority? I'm happy to put in the work and pull request if deemed a reasonable solution?</p>