Delete/remove one connections db

I have 2 connections with alias ‘default’ (config in settings) and ‘anhnt.com’ (added by after I create a record with config field)
How can I delete/remove connection ‘anhnt.com’? I tried using connections.__delitem__(‘anhnt.com’), connections.close_all() but not working.

Thank you!

You don’t - at least not while your instance is up and running.

If you’re creating connections on-the-fly and not as part of your DATABASES setting, then that connection will go away the next time the process is restarted.

I configure the database as a record:

Therefore, when this record is updated, I want to reconnect the db with the new configuration.
I tried doing this to remove old connection, and it worked:

del connections.databases[alias]
connections.close_all()
connections.__delitem__(alias)

That’ll work - until it doesn’t, and that mostly depends upon how many people may be using that connection when it gets changed, and will exhibit vastly different behavior under a production quality server (gunicorn or uwsgi) than under runserver or Werkzeug.

Thank you! I don’t know much about servers so I haven’t thought about it yet. So, there is no way I can dynamically remote database?

Not safely. It’s really a problematic thing to try and do. What you need to keep in mind is that in a production environment, you’re going to have multiple instances of the process running. What you do in one instance is not automatically carried over to the other instances.

It’s ok to dynamically add instances as long as all your code verifies that the particular instance is present when trying to use it. But if you change the properties of a connection, you’re probably going to have different instances of your system with different connection properties.

I understand. I will try it. Thank you!