How To Delete All Tabs Except One

Learn how to efficiently delete all tabs in a Google Sheets document except for one using a simple Apps Script. This tutorial covers the necessary code and explains how to handle exceptions during the process.

This video is going to have a script error and it's going to be awesome. So we've created a sheet that has a ton of tabs, hundreds of tabs. We want to delete those. You're watching this video because you want to delete all the tabs. The issue is we're going to run into a app script error , but it is fantastic because every single sheet Workbook, however you wanna call it, needs at least one tab. So we're gonna be able to delete everything, every single tab, except one that's going to allow us to write a loop for our app script that'll delete every tab except for one. So let's go and do that. So down below, I've created a tab every single week, but I don't wanna do that. I want to do function , delete tabs. parentheses curly brackets and inside our curly brackets we're going to write Automatically Delete All Sheets If Not Listed Name Writer Better Prompts How To Duplicate a Tab and Move it to the End in Google Sheets How to Prevent Delete Tab

a for loop for i equals zero this is a semicolon and i is less than let's say a hundred semicolon i plus plus this iterates this i so we're going to start i at zero we're going to iterate until i is greater than 100 and we're going to just add one to i every single time Let's create curly brackets. We're going to need a variable SS equals spreadsheet app dot get active spreadsheet . We're going to say SS dot delete sheet on which sheet are we going to delete? And we're just going to delete the very first sheet. Get Sheets, and we're going to put a parenthesis, but then a bracket and a zero. Spreadsheet Automation 101 Lesson 2: For Loop Getting Started Coding in Apps Script How to Delete Every Checked Row

What this means is it's just going to get the first sheet of the entire spreadsheet file, and it's going to delete it. We don't actually need an iterator, this I, inside here to iterate. It's going to programmatically iterate, but it's just going to delete the very first sheet every single time. Once we save it, we're going to select it up here in our drop down menu, delete tabs. If you have any other functions here, you'll have to do that, or it might already be selected and click run. When you do it, you may have to authorize it. We need to capitalize the S in Get Sheets. Save it, and let's try running again. And now we have an exception. And the exception is fantastic. It says you can't remove all the sheets in a document. So we go back to our sheet, and we have We've refreshed the sheet, and here we have only one tab left. Automatically Delete All Sheets If Not Listed Name Getting Started Coding in Apps Script Remove Second Row How to Prevent Delete Tab

Even if I want to run this again, click Run. We'll have the same exception. You can't remove all the sheets in a document. So what's interesting is this number i is less than a hundred. If you have, let's say, a couple hundred sheets, just put this to a thousand. And it'll keep running until it can't run anymore. It'll give you that exception that it can't delete all the sheets. And we now have a spreadsheet with only one sheet left. If you want to delete that one, we have to create a new sheet. And then go back and delete that other one. That's how to delete all tabs except one pretty quickly with just a few lines of code. Enjoy. And if you are not subscribed here to BetterSheets on YouTube, please do so right now because we're going to have so many more videos, so many more Automatically Delete All Sheets If Not Listed Name Figure Out Frequency of Numbers Automatically Delete All Sheets If Not Listed Name How to Delete Every Checked Row

tips, tricks, how to's, automations, fantastic stuff here on BetterSheets. Hope you'll join. Better Sheets Members Tour March 2026 Guest Posts on Better Sheets Better Sheets Videos Checklist Automated Daily Reminders