Skip to main content
Question

Removing page node suddenly not working

  • April 11, 2024
  • 3 replies
  • 133 views

Kenny_Lopez

I created a plugin that allows me to create pages, rename them, and pull in components for things like the cover. A few days ago it suddenly stopped working and now I get an error “Error: in remove: Removing this node is not allowed”. Its pretty much stopping right at the originalPage.remove() section but not sure why it would cause an error now.

const page = figma.createPage();
page.name = "Cover";
page.backgrounds = [{ type: 'SOLID', color : {r: 0, g: 0, b: 0}}];

const originalPage = figma.currentPage;
figma.currentPage= page;
originalPage.remove();

async function importNode() {
    let importComponent = await figma.importComponentByKeyAsync("b917533f747dd140e0922675938169c95e3f4456");
    let importComponent2 = await figma.importComponentByKeyAsync("e44e6e490be70ccc67b34702d4af234c765a020e");
    importComponent.createInstance();
    figma.currentPage = page2;
    importComponentOntoPage(page2, importComponent2);
    figma.currentPage = page3;
    importComponentOntoPage(page3, importComponent2);
    figma.currentPage = page4;
    importComponentOntoPage(page4, importComponent2);
    figma.currentPage = page5;
    importComponentOntoPage(page5, importComponent2);
    figma.currentPage = page;
    figma.viewport.scrollAndZoomIntoView([importComponent])
    figma.closePlugin();
}

function importComponentOntoPage(page, component) {
  const instance = component.createInstance();
  // Set the position of the component instance
  instance.x = 0; // Adjust as needed
  instance.y = 0; // Adjust as needed
  
  const frame = figma.createFrame();
  frame.name = page.name;
  instance.constraints = {
    horizontal: "STRETCH",
    vertical: "STRETCH"
};
  frame.appendChild(instance);
  page.appendChild(frame);
  frame.resize(instance.width, instance.height);
  figma.viewport.scrollAndZoomIntoView([frame]);


}

importNode();

const page2 = figma.createPage();
page2.name = "↳ 🎨 Designs";
page2.backgrounds = [{ type: 'SOLID', color : {r: 0.13333334028720856, g: 0.13333334028720856, b: 0.13333334028720856}}];

const page3 = figma.createPage();
page3.name = "↳ 🧰 Components";
page3.backgrounds = [{ type: 'SOLID', color : {r: 0.13333334028720856, g: 0.13333334028720856, b: 0.13333334028720856}}];

const page4 = figma.createPage();
page4.name = "↳ 🏖️ Sandbox";
page4.backgrounds = [{ type: 'SOLID', color : {r: 0.13333334028720856, g: 0.13333334028720856, b: 0.13333334028720856}}];

const page5 = figma.createPage();
page5.name = "↳ 🗄️ Archive";
page5.backgrounds = [{ type: 'SOLID', color : {r: 0.13333334028720856, g: 0.13333334028720856, b: 0.13333334028720856}}];

3 replies

So, this is a new thing? I’m currently building a plugin that adds a bunch of pages and then removes them but it’s not working with the same error. First time for me…


Update: this issue is intermittent and unpredictable. I haven’t found a clear pattern, but sometimes it works and sometimes it doesn’t. I’m not sure what could cause an intermittent permissions issue…


Yash_Deshpande1

I may have figured out why this is happening. 

Lets call the page you are trying to remove a PageNode: pageToRemove, and a page you may want to keep PageNode: pageToKeep.

let pageToRemove = figma.getNodeByIdAsync('some-id')
let pageToKeep = figma.getNodeByIdAsync('some-id')


Figma does not let you delete the page you are currently on. You may have recognised written the following:

 

figma.setCurrentPageAsync(pageToKeep);
pageToRemove.remove();



This method is causing a race condition, i.e. due to the async nature of your request, your first command is being run after the second command. To fix this

 

figma.setCurrentPageAsync(pageToKeep).then(()=>{pageToRemove.remove()})

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings