Unloading Ollama Models on Linux
Deprecation notice: Note that starting with Ollama v0.3.11, there is now an ollama stop
command, so you may no longer need this.
One commonly requested feature in Ollama is the ability to unload a model. Fortunately, that is extremely simple to do with some simple, very naive scripting.
As the Ollama API takes a keep_alive
argument, we can simply send a curl
request with keep_alive
set to 0
. As seding curl
requests manually can get tedious, let's write a simple script that takes the model name and unloads it. For fish
users, you can simply run funced nollama
(or whatever you want to call it, but I think that's a great name), and enter the following code.
1 2 3 4 5 6 7 8 |
|
If you prefer bash
, then write the following script somewhere on your path, and make sure you set its executable bit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Regardless which scripting language you used, and assuming you named it nollama
, you can now run nollama llama3.1
to unload the llama3.1
model. Naturally, change this to the model you wish to unload.
As you'll notice, the script is very naive, and assumes that the model was loaded and unloaded successfully. If you need to run it against a remote server, you may want to change it to respect $OLLAMA_HOST
, and if you would like to add extra checks, feel free to do so. Quick and dirty is good enough for me.
Aug. 22, 2024, 4:17 p.m.
Tags