Important: Please do not post usage questions here.
To get a quick response, please ask a question on Stack Overflow using gspread tag.
See existing questions: https://stackoverflow.com/questions/tagged/gspread
Describe the bug
I'm trying to delete some named ranges I already created in a sheet called "StaticDataSheet". However, I get this message
gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid requests[0].addNamedRange: Cannot add named range with name: Range1, a named range with that name already exists.', 'status': 'INVALID_ARGUMENT'}
This is because I decided to delete this sheet manually. When doing so, the named ranges are not explicitly deleted and remain listed.
Here is a sample

So each time, I'm trying to use this:
spread_sheet_main.list_named_ranges()
When I try to display the list, I only get an empty array [], and there is no option to delete named ranges with "#REF" errors
To Reproduce
Steps to reproduce the behavior:
- Create a new sheet with a specific name to identify it, along with an extra sheet in addition to the default "Sheet1." Then, create some named ranges.
- Verify that the named ranges were created correctly by navigating to Data → Named Ranges in the menu.
- Delete the recently created sheet.
- Check Data → Named Ranges again.
- You should now see "#REF" for the previously created "named ranges".
- In your code, try using spread_sheet_main.list_named_ranges(). You will receive an empty array. With an empty result, there is nothing available to delete.
- In the code, assume you proceed to create these "named ranges" again.
- When the program reaches the code for creating named ranges, it encounters an error due to the existing #REF entries.
gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid requests[0].addNamedRange: Cannot add named range with name: Range1, a named range with that name already exists.', 'status': 'INVALID_ARGUMENT'}
Expected behavior
if we have implemented a logic to delete we should delete without any problem
Code example*
def delete_existing_named_ranges(self, spreadsheet, range_names):
"""Delete named ranges with specified names if they already exist."""
existing_named_ranges = spreadsheet.list_named_ranges()
update_requests = []
for named_range in existing_named_ranges:
if named_range['name'] in range_names:
delete_request = {
"deleteNamedRange": {
"namedRangeId": named_range['namedRangeId']
}
}
update_requests.append(delete_request)
print(f"Deleting existing named range: {named_range['name']}")
if update_requests:
spreadsheet.batch_update({"requests": update_requests})
For further details in implementation, I created a repo with all the logic https://github.com/makubexD/namedRangesGSBug
Screenshots



Environment info:
- Operating System [Windows]:
- Python version: python --version --> Python 3.10.8
- gspread version: Version: 5.12.4
Stack trace or other output that would be helpful
Additional context
Add any other context about the problem here.
Important: Please do not post usage questions here.
To get a quick response, please ask a question on Stack Overflow using
gspreadtag.See existing questions: https://stackoverflow.com/questions/tagged/gspread
Describe the bug
I'm trying to delete some named ranges I already created in a sheet called "StaticDataSheet". However, I get this message
gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid requests[0].addNamedRange: Cannot add named range with name: Range1, a named range with that name already exists.', 'status': 'INVALID_ARGUMENT'}This is because I decided to delete this sheet manually. When doing so, the named ranges are not explicitly deleted and remain listed.
Here is a sample
So each time, I'm trying to use this:
spread_sheet_main.list_named_ranges()When I try to display the list, I only get an empty array [], and there is no option to delete named ranges with "#REF" errors
To Reproduce
Steps to reproduce the behavior:
gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid requests[0].addNamedRange: Cannot add named range with name: Range1, a named range with that name already exists.', 'status': 'INVALID_ARGUMENT'}Expected behavior
if we have implemented a logic to delete we should delete without any problem
Code example*
For further details in implementation, I created a repo with all the logic https://github.com/makubexD/namedRangesGSBug
Screenshots



Environment info:
Stack trace or other output that would be helpful
Additional context
Add any other context about the problem here.