delete
is a method. In JavaScript, a method requires opening and closing parentheses, even if there are no parameters to be passed:
context.workbook.names.getItem("Newname").delete();
My tests confirm this. Also, the code should include await.context.sync();
It's important to realize that JavaScript supports assigning a method to a variable (object), similar to assigning a function to an Excel Name
, by leaving off the parentheses. This object can then be used at some point to execute the method. This is why no errors are thrown when the parentheses are omitted.
Example:
const deleteNewName = context.workbook.names.getItem("Newname").delete;
deleteNewName();
(Thanks to Lumpenstein for this information about JS kindly provided in a comment.)