Skip to content

Instantly share code, notes, and snippets.

@alexjlockwood
Last active November 24, 2021 21:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save alexjlockwood/206f52901d089db14bd4dc7f66292dd1 to your computer and use it in GitHub Desktop.
Save alexjlockwood/206f52901d089db14bd4dc7f66292dd1 to your computer and use it in GitHub Desktop.
Deletes the selected component if it is private and unused in the file.
if (figma.currentPage.selection.length !== 1) {
figma.notify("🚫 Select a component");
return;
}
const [componentNode] = figma.currentPage.selection;
if (componentNode.type !== "COMPONENT") {
figma.notify("🚫 Select a component");
return;
}
if (componentNode.key !== '') {
// If the key is non-empty, then that means the component has
// been published as part of the team library. Deleting published
// components isn't safe because they may be used outside of the file.
figma.notify("🚫 Selected component has been published");
return;
}
// Create a list of all instances of the selected component node.
const instanceNodes = figma.root.children.flatMap(pageNode => {
return pageNode.findAll(node => {
if (node.type !== 'INSTANCE') {
// Ignore non-instance nodes.
return false;
}
// The node is an instance of the selected component node
// if its master component ID matches the component node's ID.
return node.masterComponent.id === componentNode.id;
});
}) as readonly InstanceNode[];
if (instanceNodes.length === 0) {
// The component is unused, so delete it.
componentNode.remove();
figma.notify(`🗑️ Deleted unused component`);
} else {
// The component is still in use, so don't delete it.
figma.notify(`✅ Found ${instanceNodes.length} instance(s) of the selected component`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment