Here is a third solution, it seems that specifying a sheet index for the new sheet solves the issue.
function test(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var delSheet = ss.getSheetByName("new");
if (delSheet) {
ss.deleteSheet(delSheet);
}
var sheet = ss.insertSheet(ss.getNumSheets());// create the new sheet as last sheet, you can change that if you want
sheet.setName('new');
}
EDIT :
Following HDCerberus answer that pointed the error source I suggest this code that reproduces your initial workflow (his answer does not really do so...), it uses the deleteActiveSheet
method as well (which historically was the first available method to delete a sheet, the other method came much later...)
function test2(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var delSheet = ss.getSheetByName("new");
if (delSheet) {
ss.setActiveSheet(delSheet);
ss.deleteActiveSheet();
}
ss.insertSheet("new");
}