import os
from django.core.management import BaseCommand, CommandError
from gcutils.bigquery import Client


class Command(BaseCommand):
    help = 'Removes any datasets whose tables have all expired'

    def handle(self, *args, **kwargs):
        if os.environ['DJANGO_SETTINGS_MODULE'] != \
                'openprescribing.settings.test':
            raise CommandError('Command must run with test settings')

        gcbq_client = Client().gcbq_client

        for dataset in gcbq_client.list_datasets():
            tables = list(dataset.list_tables())
            if len(tables) == 0:
                dataset.delete()

@@ -470,16 +470,26 @@
  dataset
+_list_item
  in gcbq
@@ -513,16 +513,70 @@
 sets():%0A
+            dataset_ref = dataset_list_item.reference%0A
         
@@ -593,22 +593,26 @@
  = list(
-datase
+gcbq_clien
 t.list_t
@@ -617,16 +617,27 @@
 _tables(
+dataset_ref
 ))%0A     
@@ -684,21 +684,44 @@
     
-dataset.delete(
+gcbq_client.delete_dataset(dataset_ref
 )%0A

Fix clean_up_bq_test_data