Nodes/ComfyUI-PNGInfo/Safetensors Metadata Cleaner
ComfyUI Node

Safetensors Metadata Cleaner

Strip your local paths out of a model before you share it

By galigali-san·Created 2 months ago·Updated 2 months ago· 6
Safetensors Metadata Cleaner
    • saved_path
    file
    save_namecleaned_model.safetensors
    remove_all_metadatatrue
    overwritefalse
    custom_path
    remove_keys_by_regex.*path.*|.*hash.*
    custom_metadata_json

    Here's a thing nobody tells you about sharing merged models: the .safetensors file you upload can contain your local PC's file paths. Merge tools and trainers happily record where they read their inputs from, and suddenly that checkpoint you posted carries hints about your username and drive layout - plus hashes and old merge history that are just clutter. Safetensors Metadata Cleaner exists to wipe that before you share, and it does it without ever loading the model into memory.

    How it works

    The whole trick is that it never touches the weights. A safetensors file is an 8-byte header-length, then a JSON header, then raw tensor bytes. This node reads the header, rebuilds the __metadata__ block according to your rules, writes out a new header (re-padded to the format's 8-byte alignment), then streams the tensor bytes straight from source to destination in 64MB chunks. No torch.load, no RAM spike, no GPU involved - a 12GB checkpoint is rewritten in seconds, which is exactly why this works on low-memory setups where a proper reload would die.

    Two safety details in the code are worth knowing. First, when overwrite is on, it writes a .tmp file and renames over the original only after the copy fully succeeds - so a crash mid-cleanup shouldn't destroy your model. Second, it guards against writing onto itself: if source and destination resolve to the same path without overwrite, you get a clear error instead of a corrupt file.

    The inputs that matter

    • remove_all_metadata (default True) - nuke the entire __metadata__ block. The nuclear option, and honestly the right default for most sharing.
    • remove_keys_by_regex (default .*path.*|.*hash.*) - used when remove_all_metadata is False. This selectively deletes only keys matching the regex, case-insensitively, with | for multiple patterns. So you can keep useful stuff (trigger words, trained tags) while stripping the path/hash keys that leak local info. The defaults are sensible - path and hash keys are exactly the leaky ones.
    • save_name (default cleaned_model.safetensors) - where the cleaned file goes. An extension is added automatically if you forget it.
    • overwrite (default False) - skip save_name entirely and clean the input file in place.
    • custom_path (optional) - a full file path that overrides the dropdown. This is the automation hook.
    • custom_metadata_json (optional, multiline) - a JSON object to merge into the metadata, e.g. {"author": "you"}. Good for stamping attribution back in after you've cleaned it out.

    Output is a single saved_path STRING pointing at the cleaned file.

    The automation trick

    The genuinely slick use is chaining. Any save node that outputs a file path (STRING) can feed custom_path; set overwrite to True, and the moment a model gets saved, it's auto-cleaned in the same pass. That's the workflow the README describes - no intermediate files, no manual step you'll forget. It also means you can stick this at the end of a merge workflow and get share-ready output automatically.

    Install

    Zero dependencies - pure Python stdlib on top of ComfyUI core:

    cd ComfyUI/custom_nodes
    git clone https://github.com/galigali-san/ComfyUI-PNGInfo
    

    Restart, and it's under image as Safetensors Metadata Cleaner. ComfyUI Manager finds it by the pack title too.

    Gotchas

    • The freeze is normal. Copying and rewriting a multi-gigabyte file is synchronous - ComfyUI will appear to hang for seconds to tens of seconds while it runs, then snap back. The README calls this out explicitly; it's expected behavior, not a crash. Just don't panic-kill it mid-write.
    • Make a backup for the big stuff. The .tmp-then-rename design is safe, but you're rewriting a file that took you a merge session to produce. Cheap insurance.
    • Invalid regex or malformed JSON in the respective fields raises a clear error - the node is good about telling you what broke, so read the message.
    • The dropdown lists files across your model folders, but (選択なし) is the placeholder - pick a file, fill custom_path, or the node will refuse with a helpful error.

    Between this and the pack's reader node, you get a tidy little metadata hygiene kit: inspect before you trust, clean before you share.

    Categoryimage

    Inputs (7)

    NameTypeDefaultDescription
    fileCOMBO2 options: (選択なし), (safetensorsが見つかりません)
    save_nameSTRINGcleaned_model.safetensors
    remove_all_metadataBOOLEANtrue
    overwriteBOOLEANfalseTrueの場合、save_nameは無視され、元ファイルを直接上書きクリーンアップします。既存の保存ノードから出力されたパスを処理する際に便利です。
    custom_pathoptSTRING既存の保存ノードの出力パス(STRING型)をここに繋ぐと、自動でそのファイルを処理します。
    remove_keys_by_regexoptSTRING.*path.*|.*hash.*remove_all_metadataがFalseの場合に、このパターンにマッチするキーを削除します。パイプ(|)で複数指定可。
    custom_metadata_jsonoptSTRING追加または上書きしたいメタデータをJSON形式で入力します。

    Outputs (1)

    NameTypeDescription
    saved_pathSTRING