Destroying components using the destory() function

One can destroy components manually by using the component.destory() function. The following code shows an example of destroying a component with a specified ID:

var componentToDestory = component.find("newCmp");//newCmp is the aura:id of the component to destroycomponentToDestory.destroy();
After a component that is declared in the markup is no longer in use, the framework automatically destroys it and frees up its memory. If you create a component dynamically in JavaScript and that component isn't added to a facet (v.body, or another attribute of the type Aura.Component[]), you have to destroy it manually by using destroy(), to avoid memory leaks. 

Get Learning Salesforce Lightning Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.