{"id":2391,"date":"2026-10-26T07:47:25","date_gmt":"2026-10-25T23:47:25","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2391"},"modified":"2026-03-29T10:12:32","modified_gmt":"2026-03-29T10:12:32","slug":"how-to-use-rangedelete-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangedelete-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.Delete in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Delete<\/code> method of the Range object in Excel is a powerful tool for removing cells, rows, or columns from a worksheet, shifting the surrounding cells to fill the space. In xlwings, this functionality is exposed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model. Understanding how to use <code>Delete<\/code> effectively is essential for data manipulation and cleanup tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The <code>Delete<\/code> method removes the specified range and shifts adjacent cells. The direction of the shift is determined by the <code>Shift<\/code> argument. This is useful for deleting unwanted data, removing blank rows\/columns, or dynamically restructuring a worksheet. Unlike simply clearing contents, <code>Delete<\/code> physically removes the cells, altering the worksheet layout.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, the syntax for calling the <code>Delete<\/code> method is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range.api.Delete(Shift)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Where <code>range<\/code> is an xlwings Range object, and <code>Shift<\/code> is an optional parameter that specifies how to shift cells to replace the deleted ones. The <code>Shift<\/code> parameter can be one of the following constants from the <code>xlwings.constants<\/code> module (or their equivalent numeric values):<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Constant (from xlwings.constants)<\/th><th class=\"has-text-align-left\" data-align=\"left\">Value<\/th><th class=\"has-text-align-left\" data-align=\"left\">Description<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>xlShiftToLeft<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">-4159<\/td><td class=\"has-text-align-left\" data-align=\"left\">Shifts cells to the left.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>xlShiftUp<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">-4162<\/td><td class=\"has-text-align-left\" data-align=\"left\">Shifts cells upward.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If the <code>Shift<\/code> argument is omitted, Excel will default to shifting cells up (<code>xlShiftUp<\/code>) for entire rows and to the left (<code>xlShiftToLeft<\/code>) for entire columns. For smaller ranges that are not entire rows\/columns, Excel uses the default based on the shape of the range.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Examples:<\/strong><br>Here are several xlwings API code instances demonstrating the use of <code>Range.Delete<\/code>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Delete a single cell and shift up:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=False)\nwb = app.books.open('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n# Delete cell B2 and shift the cells below it upward\nsheet.range('B2').api.Delete()\nwb.save()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Delete a range of rows:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nfrom xlwings.constants import DeleteShiftDirection\napp = xw.App(visible=False)\nwb = app.books.open('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n# Delete rows 5 through 7, shifting cells up\nsheet.range('5:7').api.Delete(Shift=DeleteShiftDirection.xlShiftUp)\nwb.save()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Delete a column and shift left:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=False)\nwb = app.books.open('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n# Delete column C, shifting columns to the right of it leftward\nsheet.range('C:C').api.Delete()\nwb.save()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Delete a non-contiguous range:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=False)\nwb = app.books.open('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n# Delete cells A1:B2 and D1:D3, shifting cells up for each area\ndelete_range = sheet.range('A1:B2, D1:D3')\ndelete_range.api.Delete()\nwb.save()\napp.quit()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Delete` method of the Range object in Excel is a powerful tool for removing cells, rows, or col&#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-2391","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2391","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=2391"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2391\/revisions"}],"predecessor-version":[{"id":3639,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2391\/revisions\/3639"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}