Skip to content

Remove deleted clipboard images from disk and from the clipboard - #18809

Closed
ThePrimeagen wants to merge 3 commits into
omacom:quattrofrom
ThePrimeagen:fix/d88dbf0e-67fe-4a23-960a-1dae5adb604f
Closed

ThePrimeagen wants to merge 3 commits into
omacom:quattrofrom
ThePrimeagen:fix/d88dbf0e-67fe-4a23-960a-1dae5adb604f

Conversation

@ThePrimeagen

Copy link
Copy Markdown
Contributor

Deleting an image in clipboard history (Super+Ctrl+V, then Delete or Shift+Delete) only rewrites clipboard-history.json in Clipboard.qml: the image's file stays in ~/.local/state/omarchy/clipboard-images, and the current copy stays on the clipboard. Images dropped at the 500-entry history limit leave their files behind too.

Screenshots

Before After
before after

Reproduce

  1. Press Super+Enter to open a terminal.
  2. Run qrencode -o /tmp/a.png hello && wl-copy --type image/png < /tmp/a.png to copy an image.
  3. Press Super+Ctrl+V: the image is the selected entry.
  4. Press Delete: the panel shows "Clipboard is empty". Press Escape.
  5. Run ls ~/.local/state/omarchy/clipboard-images; wl-paste --list-types: the image's file is still there, and the clipboard still offers image/png.

More cases

Shift+Delete, clearing the whole history

Before After
before after

Copying an image when history already holds 500 entries: the file count grows on the nightly and stays put with the change

Before After
before after

Related

Image history entries store their bytes in clipboard-images/, but
deleting, clearing, or evicting an entry only rewrote
clipboard-history.json, leaving the file on disk and, for the current
copy, the live Wayland clipboard serving it.

Add ClipboardHistory.removedImagePaths to compute image paths dropped
between two histories, and Clipboard.pruneRemovedImages to unlink
those paths under clipboard-images/ and run wl-copy --clear when the
removed entry was the current one or all history was cleared. Call it
from addClipboardEntry (eviction at the 500 limit, without clearing
the new current clipboard), removeDisplayIndex (clearing only when
history index 0 was deleted, so an older image deletion leaves the
newer clipboard untouched), and confirmClearHistory.
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes stay within the desktop clipboard-history workflow.

The PR still needs capture and cleanup to stop overlapping before it is safe to merge.

Findings

  1. P1 Cleanup deletes a returning image ▶

Summary

Removes image files when users delete clipboard entries, clear history, or reach the history limit.

  • Deleting clipboard images clears matching copies and cleans up unused files.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[History changes] --> B[Find removed image paths]
  B --> C{User deleted entries?}
  C -->|Yes| D[Hash live image once]
  D --> E[Clear clipboard if hash matches]
  C -->|No| F[Wait five seconds]
  B --> F
  F --> G[Read saved history]
  G --> H[Delete files still absent]
Loading

Reviews (3) · Last reviewed commit: "Prune removed images from a process a sh..." · Reviewed by Greptile

Comment thread shell/plugins/clipboard/Clipboard.qml Resolved
Comment thread shell/plugins/clipboard/Clipboard.qml Outdated
Comment on lines +85 to +86
if (path.indexOf(root.imageDir + "/") === 0)
Quickshell.execDetached(["rm", "-f", path])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Cleanup deletes a returning image

Image cleanup can delete a file that a capture is about to add back. Image names come from their bytes, and capture.sh discards its temporary copy when that file already exists. At the history limit, a preceding capture can evict that image and start rm while its re-capture prepares its JSON output. The re-capture then adds a path whose file was deleted, breaking its preview and later copy. Coordinate capture and cleanup so checking, publishing, and deleting the shared file cannot overlap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7fdbec7: removed files are no longer deleted right away. They wait in pendingImageRemovals, and when a five-second one-shot timer (restarted on each removal) fires, only the files removedImagePaths(pending, root.history) still reports are deleted. A re-capture that found A's file reports A within milliseconds, so A is back in history by then and its file is kept. One limit: a shell that exits within those five seconds leaves those files behind.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 6774b80, the deferred delete runs in a detached process and checks the saved history. Why this closes the race: after capture.sh finds A's file, all it has left is rm of its temp copy, one date and one jq. The shell adds the line the moment it reads it and saves history at once. So A is back in the saved history long before the five-second prune reads it, and A's file is kept. The decision to delete is made against the history as it stands then, not the history at the moment B evicted A.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about the interleaving I flagged: after a re-capture is handled, saveHistory() persists A, and the detached prune checks that current history after five seconds, so it keeps A's file. My previous comment described unconditional deletion and no longer applies to this version; I consider the finding addressed.

Strictly, the five-second grace period isn't an atomic guarantee: a capture could find the existing file and then be stalled for more than five seconds before its JSON is handled and history saved. Given the remaining capture work is just temp cleanup, date, and jq, that's a narrow pathological delay rather than the ordinary race I raised.

Deleting the newest history entry ran wl-copy --clear on the guess that
it was still on the clipboard. A password manager's copy is never
recorded, so the older image stayed newest and deleting it wiped the
password. Each image file is named for the sha256 of its bytes, so the
clipboard is now cleared only when its image hashes to the deleted
file's name.

A capture that finds an image's file already on disk drops its own copy
and reports the file a moment later, so an eviction in between could
delete the file it was about to add back. Removed files now wait five
seconds and are deleted only if the history still does not hold them.

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread shell/plugins/clipboard/Clipboard.qml Resolved
Comment thread shell/plugins/clipboard/Clipboard.qml Resolved
Removed image files waited five seconds in the shell's memory, so a
shell restart in that window left them on disk for good. A detached
process now waits instead and deletes each file the saved history still
does not hold.

Clearing history read and hashed the clipboard once per removed image.
It now reads it once and compares the hash against every removed file.

Co-authored-by: Cursor <cursoragent@cursor.com>
@omarchybot

Copy link
Copy Markdown
Collaborator

Closing in favour of #14266, an earlier contributor PR for the same clipboard image cleanup. The deletion here races a re-capture of the same image after its sleep, deletes every candidate when the jq read fails, and adds a separate clearing of the live clipboard that races too.

Reviewed by Claude Opus 5.5 with Codex Medium as a second opinion; its independence is not guaranteed.

@omarchybot omarchybot closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants