santa hat
deerdeer nosedeer glow
Sign In

How do batch delete old model files if an new version is present

Hi I just wanna ask is there a way to batch del old model files like (.safetensors) if a new one is present or it is all done manually ..

If someone can create a script that can just be downloaded and run and then creates a file with that information then it gives you all the ones in there you can select to deal with them it would be helpful .. and you can re-run it in future .. something like the one below but it doesn't work ..

import os
import json

# Set the directory path where the .safetensors files are located
directory = r"C:\AI\stable-diffusion-webui"

# Create a list to store the information about .safetensors files
file_list = []

# Scan the directory and subdirectories for .safetensors files and collect their information
for root, dirs, files in os.walk(directory):
    for file in files:
        if file.endswith(".safetensors"):
            file_path = os.path.join(root, file)
            with open(file_path, "r") as f:
                data = json.load(f)
                file_info = {
                    "file": file_path,
                    "versions": list(data.keys())

# Display the list of .safetensors files and their versions
for i, file_info in enumerate(file_list, start=1):
    print(f"{i}. {file_info['file']}")
    for version in file_info["versions"]:
        print(f"   - {version}")

# Prompt user to select a file and version to delete
file_index = int(input("Enter the number of the file to delete a version from: ")) - 1
version_to_delete = input("Enter the version to delete: ")

# Delete the specified version from the selected .safetensors file
selected_file = file_list[file_index]["file"]
with open(selected_file, "r") as f:
    data = json.load(f)
    if version_to_delete in data:
        del data[version_to_delete]
        with open(selected_file, "w") as f:
            json.dump(data, f)
        print(f"Deleted version '{version_to_delete}' from file '{selected_file}'.")
        print(f"Version '{version_to_delete}' does not exist in file '{selected_file}'.")

input("Press Enter to exit...")

1 Answer

You've had a chatbot write this, haven't you. It's obviously completely ignorant of the model files' infrastructure, of course it wouldn't work.

There is no version (that I know of) in models' metadata, as the extra networks tab shows. There's a hash and some training parameters, that's it.

As such, there's an easy way and a hard way to do what you want.

The easy way is to search locally for files whose names are very similar. Dupeguru does this okay. A chatbot should be able to write such a basic program in python / batch.

The hard way is to search for each model's hash in civ, and connect it to its other versions (and either search & delete on the spot or defer it until full scan). Would have to use the api like other extensions do along with some standard bookkeeping on the backend.

Bear in mind that the hard way is flawed even though it's exactly per your specifications: it will delete model sets (ie different content with a similar theme, like characters from the same anime), it will fail to delete models whose versions have been published across different posts, it has no notion of version quality difference like lora-lyco-loha, or experimental upgrade.

Ultimately, having a standard naming scheme of model + author + version would be the best for being able to understand what the model does and what its quality level is quickly for a manual or automated review. However, when I brought up the issue I was ignored, since the admins & users believe being able to copypasta the prompt from a sample and it'll work without having to change the lora's name, is more important than being able to search for it the next day.

Your answer