{"id":2258,"date":"2026-08-20T15:31:06","date_gmt":"2026-08-20T07:31:06","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2258"},"modified":"2026-03-28T12:01:03","modified_gmt":"2026-03-28T12:01:03","slug":"how-to-use-worksheetsdelete-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetsdelete-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheets.Delete in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Delete<\/code> member of the Worksheets collection in the Excel object model is a method used to remove a specified worksheet from a workbook. In xlwings, this functionality is exposed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model, allowing for precise control over worksheet management. This method is particularly useful for automating the cleanup of temporary sheets, restructuring workbooks dynamically, or removing unnecessary data sheets in batch processing scenarios. Understanding its usage in xlwings ensures efficient workbook manipulation while maintaining compatibility with Excel&#8217;s native behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The <code>Delete<\/code> method permanently deletes a worksheet from the workbook. It does not move the sheet to the Recycle Bin; once deleted, the sheet cannot be recovered unless the workbook is closed without saving. In xlwings, this operation is performed by accessing the Excel API, so it mirrors the exact behavior of Excel&#8217;s VBA <code>Delete<\/code> method. This includes any prompts or warnings Excel might display, depending on the application settings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, the <code>Delete<\/code> method is called via the <code>api<\/code> property on a Worksheet object. The syntax is:<br><code>worksheet.api.Delete()<\/code><br>Here, <code>worksheet<\/code> refers to an xlwings Worksheet object (e.g., obtained through <code>wb.sheets['SheetName']<\/code> or <code>wb.sheets[0]<\/code>). The method does not take any parameters in its basic form. However, in Excel&#8217;s object model, the <code>Delete<\/code> method can be influenced by the application&#8217;s display alerts. To suppress confirmation dialogs, you can set <code>xlwings.App().api.DisplayAlerts = False<\/code> before deletion and restore it to <code>True<\/code> afterward.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><br>Below is a code example demonstrating the usage of the <code>Delete<\/code> member with xlwings. This example assumes you have an existing workbook with multiple sheets and want to remove a specific sheet programmatically.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to an existing workbook (adjust the path as needed)\nwb = xw.Book('example.xlsx')\n\n# Access a specific worksheet by name\nsheet_to_delete = wb.sheets&#91;'TempSheet']\n\n# Disable Excel alerts to avoid confirmation prompts\napp = xw.apps.active\napp.api.DisplayAlerts = False\n\n# Delete the worksheet\nsheet_to_delete.api.Delete()\n\n# Re-enable alerts\napp.api.DisplayAlerts = True\n\n# Save the workbook to persist changes\nwb.save()\n\n# Close the workbook (optional)\nwb.close()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Delete` member of the Worksheets collection in the Excel object model is a method used to remov&#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-2258","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2258","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=2258"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2258\/revisions"}],"predecessor-version":[{"id":3448,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2258\/revisions\/3448"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}