Search code examples
python-3.xgoogle-sheets-apigspread

How to delete the complete spreadsheet using gspread, python


Explanation:

  • I got into a situation where I just had to delete the SpreadSheet after the work is finished.

  • I went through the documentation and I failed to find the function to delete the SpreadSheet.

Code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials


def update_data():
    return

def export_data():
    return

def delete_sheet():
    # ----------------------------------------------------
    # DELETE SHEET HERE
    # ----------------------------------------------------
    return

if __name__ == "__main__":
    scope = ["https://spreadsheets.google.com/feeds",
             "https://www.googleapis.com/auth/spreadsheets",
             "https://www.googleapis.com/auth/drive.file",
             "https://www.googleapis.com/auth/drive"]
    creds = ServiceAccountCredentials.from_json_keyfile_name('Client_secret.json', scope)
    client = gspread.authorize(creds)

    url = "Url of my SpreadSheet"
    sheet = client.open_by_url(url).sheet1

    update_data(sheet)
    export_data(sheet)
    delete_sheet(sheet)

Solution

  • You can delete a spreadsheet with Client.del_spreadsheet() method.