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
function nollama
    if not set -q $argv[1]
        echo "Error: No model specified. Usage: $(status current-command) <model>"
        return 1
    end
    curl http://localhost:11434/api/generate -d '{"model": "'$argv[1]'", "keep_alive": 0}' >/dev/null 2>&1
    echo Unloaded model $argv[1]
end

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
#!/bin/bash

# Get the first argument
MODEL=$1

if [ -z $MODEL ]; then
  echo "Error: No model specified. Usage: $0 <model>"
  exit 1
fi

curl http://localhost:11434/api/generate -d '{"model": "'$MODEL'", "keep_alive": 0}' >/dev/null 2>&1

echo Unloaded model $MODEL

exit 0

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

Ollama