Delete a Dataset from BigQuery using Python

In this tutorial, we will learn how to delete a dataset from BigQuery using Python. This is a common cleanup operation, especially when working with temporary data environments.

Install the BigQuery Python Client

Install the required BigQuery client library before running the Python code:

pip install google-cloud-bigquery    

Python Code to Delete Dataset

We will use the delete_dataset method from the google-cloud-bigquery library.

Python

# Import the packages
from dotenv import load_dotenv
import os
from google.oauth2 import service_account
from google.cloud import bigquery

def main():
    # Loads environment variables from a .env file
    load_dotenv()

    # Read environment variables
    credentials_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
    project_name = os.getenv("project_id")

    # Create credentials using the service account file
    credentials = service_account.Credentials.from_service_account_file(credentials_path)

    # Create the BigQuery client
    client = bigquery.Client(credentials=credentials, project=project_name)

    # Define the dataset ID
    dataset_id = "ashishcoder.new_dataset"

    # Delete the dataset
    # delete_contents=True removes all tables and views inside the dataset
    # not_found_ok=True prevents error if dataset does not exist
    client.delete_dataset(dataset_id, delete_contents=True, not_found_ok=True)

    # Print a success message
    print(f"Delete dataset {dataset_id}.")

if __name__ == "__main__":
    main()    

Key Parameters Explained

Note: Deleting a dataset is permanent. All data within the dataset will be lost once deleted with delete_contents=True.