I'm trying to clear up some space in my Google service account. I've been using it to handle spreadsheets and I want to delete some spreadsheets that I have created earlier. To delete a spreadsheet, there if function as gc.del_spreadsheet(file_id) in gspread. However, I could not find a way to retrieve the files ids of the spreadsheets that I want to delete.
I could not find a way to open the google drive of the service account like a google drive of a personal account. Therefore, I used the following code to list all the spreadsheets in the account as of now. The code outputs the spreadsheet title but not the file id which is required to delete the spreadsheet.
def upload_to_google_sheets():
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name("client.json", scope)
gc = gspread.authorize(credentials)
print('authorized')
titles_list = []
for spreadsheet in gc.openall():
titles_list.append(spreadsheet.title)
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(titles_list)
upload_to_google_sheets()
The final objective is to delete spreadsheets using gspread del_spreadsheet function which requires "file_id".
If my understanding is correct, how about this modification?
titles_list.append(spreadsheet.title)
titles_list.append({'title': spreadsheet.title, 'id': spreadsheet.id})
spreadsheet
of for spreadsheet in gc.openall():
includes the Spreadsheet ID.spreadsheet.id
.By this modification, you can retrieve the following result.
[ { 'id': '### Spreadsheet ID1 ###',
'title': '### Spreadsheet name1 ###'},
{ 'id': '### Spreadsheet ID2 ###',
'title': '### Spreadsheet name2 ###'},
,
,
,
]
If I misunderstood your question and this was not the result you want, I apologize.