{"id":2280,"date":"2026-08-31T16:40:39","date_gmt":"2026-08-31T08:40:39","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2280"},"modified":"2026-03-28T12:17:31","modified_gmt":"2026-03-28T12:17:31","slug":"how-to-use-worksheetdelete-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetdelete-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Delete in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Delete<\/code> member of the <code>Worksheet<\/code> object in the Excel object model, accessible via the xlwings API in Python, provides a programmatic way to remove a specific worksheet from a workbook. This operation is irreversible through the API itself (akin to manually deleting a sheet and not saving the workbook), so it should be used with caution, especially on unsaved workbooks where data loss can occur.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The primary function of the <code>Delete<\/code> method is to permanently delete the worksheet object on which it is called. After deletion, the worksheet is removed from the workbook&#8217;s <code>Worksheets<\/code> collection. If the deleted sheet was the only sheet in the workbook, xlwings and Excel will typically prevent its deletion to maintain at least one visible sheet, as an Excel workbook must contain at least one visible worksheet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax &amp; Parameters<\/strong><br>In xlwings, the method is called directly on a <code>Sheet<\/code> object (which represents a worksheet or chart sheet). The xlwings API abstracts the underlying COM calls into a simple Python method call.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sheet.delete()<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are no parameters for this method in the standard xlwings API. The action is applied to the sheet object referenced. It&#8217;s important to note that the <code>Sheet<\/code> object in xlwings corresponds to what the Excel Object Model calls a <code>Worksheet<\/code> (for standard worksheets) or a <code>Chart<\/code> object (for chart sheets). The <code>delete()<\/code> method works on both.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Basic Deletion of a Specific Sheet:<\/strong><br>This example opens a workbook and deletes a sheet named &#8220;SheetToRemove&#8221;.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Open the workbook (use full path if needed)\nwb = xw.Book(\"example.xlsx\")\n\n# Access the specific worksheet\nsheet_to_delete = wb.sheets&#91;\"SheetToRemove\"]\n\n# Delete the worksheet\nsheet_to_delete.delete()\n\n# Save the workbook to persist the change\nwb.save()\nwb.close()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Deleting the Active Sheet:<\/strong><br>This example deletes whichever sheet is currently active in the open workbook.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\napp = xw.App(visible=False) # Start Excel in the background\nwb = app.books.open(\"data.xlsx\")\n\n# Delete the active sheet\nwb.sheets.active.delete()\n\nwb.save(\"data_modified.xlsx\")\nwb.close()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Conditional Deletion Based on Content:<\/strong><br>A more practical example involves checking sheet names or content before deletion. This script deletes all sheets whose name contains the word &#8220;Temp&#8221;.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\nwb = xw.Book(\"report.xlsx\")\n\n# Create a list of sheets to delete first to avoid iteration issues\nsheets_to_delete = &#91;sht for sht in wb.sheets if \"Temp\" in sht.name]\n\nfor sht in sheets_to_delete:\n    print(f\"Deleting sheet: {sht.name}\")\n    sht.delete()\n\nwb.save()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Delete` member of the `Worksheet` object in the Excel object model, accessible via the xlwings &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2280","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2280"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2280\/revisions"}],"predecessor-version":[{"id":3480,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2280\/revisions\/3480"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}