Repository: googleapis/google-cloud-python

Issue:
BigQuery: add argument to delete_dataset for deleteContents parameter
The [delete verb for datasets](https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/delete) has a `deleteContents` parameter.

> If True, delete all the tables in the dataset. If False and the dataset contains tables, the request will fail. Default is False

We do not currently expose this parameter in [delete_dataset()](https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/reference.html#google.cloud.bigquery.client.Client.delete_dataset).

I could see this parameter being very handy in integration and snippets tests, where we want to delete a dataset and all the tables we have created.


Discussion:

Resolving patch:
diff --git a/bigquery/google/cloud/bigquery/client.py b/bigquery/google/cloud/bigquery/client.py
--- a/bigquery/google/cloud/bigquery/client.py
+++ b/bigquery/google/cloud/bigquery/client.py
@@ -446,7 +446,8 @@ def list_dataset_tables(self, *args, **kwargs):
             DeprecationWarning)
         return self.list_tables(*args, **kwargs)
 
-    def delete_dataset(self, dataset, retry=DEFAULT_RETRY):
+    def delete_dataset(self, dataset, delete_contents=False,
+                       retry=DEFAULT_RETRY):
         """Delete a dataset.
 
         See
@@ -455,15 +456,27 @@ def delete_dataset(self, dataset, retry=DEFAULT_RETRY):
         :type dataset: One of:
                        :class:`~google.cloud.bigquery.dataset.Dataset`
                        :class:`~google.cloud.bigquery.dataset.DatasetReference`
+        :param dataset: the dataset to delete, or a reference to it.
 
         :type retry: :class:`google.api_core.retry.Retry`
         :param retry: (Optional) How to retry the RPC.
 
-        :param dataset: the dataset to delete, or a reference to it.
+        :type delete_contents: boolean
+        :param delete_contents: (Optional) If True, delete all the tables
+             in the dataset. If False and the dataset contains tables, the
+             request will fail. Default is False
         """
         if not isinstance(dataset, (Dataset, DatasetReference)):
             raise TypeError('dataset must be a Dataset or a DatasetReference')
-        self._call_api(retry, method='DELETE', path=dataset.path)
+
+        params = {}
+        if delete_contents:
+            params['deleteContents'] = 'true'
+
+        self._call_api(retry,
+                       method='DELETE',
+                       path=dataset.path,
+                       query_params=params)
 
     def delete_table(self, table, retry=DEFAULT_RETRY):
         """Delete a table


Test patch:
diff --git a/bigquery/tests/system.py b/bigquery/tests/system.py
--- a/bigquery/tests/system.py
+++ b/bigquery/tests/system.py
@@ -215,6 +215,31 @@ def test_create_table(self):
         self.assertTrue(_table_exists(table))
         self.assertEqual(table.table_id, table_id)
 
+    def test_delete_dataset_delete_contents_true(self):
+        dataset_id = _make_dataset_id('delete_table_true')
+        dataset = retry_403(Config.CLIENT.create_dataset)(
+            Dataset(Config.CLIENT.dataset(dataset_id)))
+
+        table_id = 'test_table'
+        table_arg = Table(dataset.table(table_id), schema=SCHEMA)
+        table = retry_403(Config.CLIENT.create_table)(table_arg)
+        Config.CLIENT.delete_dataset(dataset, delete_contents=True)
+
+        self.assertFalse(_table_exists(table))
+
+    def test_delete_dataset_delete_contents_false(self):
+        from google.api_core import exceptions
+        dataset_id = _make_dataset_id('delete_table_false')
+        dataset = retry_403(Config.CLIENT.create_dataset)(
+            Dataset(Config.CLIENT.dataset(dataset_id)))
+
+        table_id = 'test_table'
+        table_arg = Table(dataset.table(table_id), schema=SCHEMA)
+
+        retry_403(Config.CLIENT.create_table)(table_arg)
+        with self.assertRaises(exceptions.BadRequest):
+            Config.CLIENT.delete_dataset(dataset)
+
     def test_get_table_w_public_dataset(self):
         PUBLIC = 'bigquery-public-data'
         DATASET_ID = 'samples'
diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py
--- a/bigquery/tests/unit/test_client.py
+++ b/bigquery/tests/unit/test_client.py
@@ -1117,6 +1117,22 @@ def test_delete_dataset(self):
             req = conn._requested[0]
             self.assertEqual(req['method'], 'DELETE')
             self.assertEqual(req['path'], '/%s' % PATH)
+            self.assertEqual(req['query_params'], {})
+
+    def test_delete_dataset_delete_contents(self):
+        from google.cloud.bigquery.dataset import Dataset
+
+        PATH = 'projects/%s/datasets/%s' % (self.PROJECT, self.DS_ID)
+        creds = _make_credentials()
+        client = self._make_one(project=self.PROJECT, credentials=creds)
+        conn = client._connection = _Connection({}, {})
+        ds_ref = client.dataset(self.DS_ID)
+        for arg in (ds_ref, Dataset(ds_ref)):
+            client.delete_dataset(arg, delete_contents=True)
+            req = conn._requested[0]
+            self.assertEqual(req['method'], 'DELETE')
+            self.assertEqual(req['path'], '/%s' % PATH)
+            self.assertEqual(req['query_params'], {'deleteContents': 'true'})
 
     def test_delete_dataset_wrong_type(self):
         creds = _make_credentials()