Repository: Cog-Creators/Red-DiscordBot

Issue:
Error while deleting all data of an instance
# Other bugs

#### What were you trying to do?

I've tried to delete all data of my second instance that use PostgreSQL.

#### What were you expecting to happen?

Deleting all data

#### What actually happened?

Here is the traceback :

```
Traceback (most recent call last):
  File "/home/julien/red/bin/redbot-setup", line 8, in <module>
    sys.exit(run_cli())
  File "/home/julien/red/lib/python3.8/site-packages/redbot/setup.py", line 429, in run_cli
    cli()  # pylint: disable=no-value-for-parameter  # click
  File "/home/julien/red/lib/python3.8/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/julien/red/lib/python3.8/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/julien/red/lib/python3.8/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/julien/red/lib/python3.8/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/julien/red/lib/python3.8/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/julien/red/lib/python3.8/site-packages/redbot/setup.py", line 372, in delete
    loop.run_until_complete(
  File "/home/julien/.pyenv/versions/3.8.1/lib/python3.8/asyncio/base_events.py", line 612, in run_until_complete
    return future.result()
  File "/home/julien/red/lib/python3.8/site-packages/redbot/setup.py", line 258, in remove_instance
    await driver_cls.delete_all_data(interactive=interactive, drop_db=drop_db)
  File "/home/julien/red/lib/python3.8/site-packages/redbot/core/drivers/postgres/postgres.py", line 246, in delete_all_data
    await cls._pool.execute(fs.read())
AttributeError: 'NoneType' object has no attribute 'execute'
```

#### How can we reproduce this issue?

Create an instance that use PostgreSQL, then try to delete it.



Discussion:

Resolving patch:
diff --git a/redbot/setup.py b/redbot/setup.py
--- a/redbot/setup.py
+++ b/redbot/setup.py
@@ -253,20 +253,23 @@ async def remove_instance(
 
     backend = get_current_backend(instance)
     driver_cls = drivers.get_driver_class(backend)
+    await driver_cls.initialize(**data_manager.storage_details())
+    try:
+        if delete_data is True:
+            await driver_cls.delete_all_data(interactive=interactive, drop_db=drop_db)
 
-    if delete_data is True:
-        await driver_cls.delete_all_data(interactive=interactive, drop_db=drop_db)
-
-    if interactive is True and remove_datapath is None:
-        remove_datapath = click.confirm(
-            "Would you like to delete the instance's entire datapath?", default=False
-        )
+        if interactive is True and remove_datapath is None:
+            remove_datapath = click.confirm(
+                "Would you like to delete the instance's entire datapath?", default=False
+            )
 
-    if remove_datapath is True:
-        data_path = data_manager.core_data_path().parent
-        safe_delete(data_path)
+        if remove_datapath is True:
+            data_path = data_manager.core_data_path().parent
+            safe_delete(data_path)
 
-    save_config(instance, {}, remove=True)
+        save_config(instance, {}, remove=True)
+    finally:
+        await driver_cls.teardown()
     print("The instance {} has been removed\n".format(instance))
 
 


Test patch: