Remove unused attachments

Its hard to delete every attachment after deleting a file. And if there are lot of files in attachment folder its hard to find that specific attachment
Maybe while deleting file there should be pop up asking if user wants to delete attachments too

39 Likes

I’d like to see this as well. For example a button you could press and remove attachments that aren’t used.

This has previously been suggested as a plugin here: Delete unused media

3 Likes

I believe it would be extremely useful to have an option to remove all media associated with a note/file if there are not any active references to it in any other notes.

I tend to regularly drag images (screenshots, etc.) into notes when in meetings, and while I can certainly iterate over all non-.md files in the vault, grep for their presence in all of the notes, etc., that’s not necessarily something that all users will have the skills to do, or will even feel comfortable doing.

5 Likes

Use case or problem

There is a note. It has embedded files. These are usually images, possibly sound, etc.
Now, when you delete a note, all embedded files remain in the storage, which leads to a littering of the system

Proposed solution

You need an additional feature so that when you delete a note, the embedded files are also deleted

Current workaround (optional)

You have to delete these files manually.

Related feature requests (optional)

3 Likes

There should be an automagic way to remove orphan attachments.
I suggest the following: when deleting a note which contains the last link to an attachment, there should be a warning After deletion of this note the attachment ... will become orphan. Delete attachment ...? (Yes/No)

14 Likes

Same demands, that’s really a natual but missing feature.

I think my plugin find unlinked files is a good solution. It finds files (also attachments) which are nowhere linked (orphans) and lists them in an output file. With an extra command you can delete all files listed there with a certain extension. You can choose which extensions to delete in the settings. In this case it could be .jpg

6 Likes

This feature is critical for those who are bringing a large legacy database from Evernote (say) to markdown.

The .enex export contains a MYRIAD useful and useless files that were attached to the original HTML clippings, which are then imported to .md (using the life-saving application yarle).

My obsidian vault now contains 1000s of such useless files linked from the _resources folder to the .md files, and as I clean up the HTML advertising and recirculation tags in the .md files, they are all just lying there in _resources.

Any such feature rollout should (ideallly) not only delete orphans as they form, BUT be able to delete legacy orphan files (such as mine).

PLEASE PLEASE PRETTY PLEASE :pray:

2 Likes

I would like to check before deleting, so I hoped for creating a Dataview, but that plugin seems unable to see anything apart from *.md:

```dataview
list
from "Anhänge"
```

Dataview: Query returned 0 results.

(where “Anhänge” is just the German word for “attachments”).

1 Like

This is a very useful plugin. I store all my screenshots in a central assets folder. Over time, it accumulates quite a few orphans. This plugin did a great job identifying the orphaned jpg files and deleting them from my central assets folder.

1 Like

I made a fix for this. In the following example, we will remove all the excalidraw drawings that are not linked by another note.

Make a note with the title “Delete excalidraw orphans” and the following body:

```dataviewjs
dv.list(dv.pages().file.where(f => f.name.startsWith("Drawing ") && f.name.endsWith("excalidraw")).where(f => f.inlinks == 0 && f.outlinks == 0).path.map(path => "<%+ tp.user.delete_file({FILE_TO_REMOVE:'"+path+"'}) %>"))
\`\`\`

Make a user command using (python in my case) that will delete the files:

#!/usr/bin/env python3

import os

if os.name == 'nt':
    vault_root_dir = "D:/GOOGLE DRIVE/OBSIDIAN/NOTES"
else:
    exit

os.remove(os.path.join(vault_root_dir, os.environ['FILE_TO_REMOVE']))

Just for reference, my user command in the templater config is:
c:/python39/python.exe "D:/GOOGLE DRIVE/OBSIDIAN/NOTES/💎 OBSIDIAN/SCRIPTS/delete_file_by_path.py"

Now whenever you render the note, your orphan excalidraw drawings will be autoremoved.
Now I would like to run this as a single command instead of having to render the note so I can put it fully into a template (So this can be called via the templates menu instead of having to go to the note and render it).
Hopefully someone here knows how to do that :slight_smile:

Ok, I simplified this a lot, you just need to run the following template and your files will be deleted:

<%*

const dv = this.app.plugins.plugins["dataview"].api
const filesArray = dv.pages().file.where(f => f.name.startsWith("Drawing ") && f.name.endsWith("excalidraw")).where(f => f.inlinks == 0 && f.outlinks == 0).path
filesArray.map(path => {
tp.user.delete_file({FILE_TO_REMOVE: path})
})

%>
1 Like

Recently testing Obsidian and came here for this. Couple of thoughts:

  1. For removing previously orphaned files, I think the plugin is fine.
  2. Upon note deletion, removal of orphaned attachments should absolutely be core functionality.
1 Like

Fully agree with everyone that believes removal of attachments should be done automatically upon deleting the note its attached too.

I’d also wish to have such a feature.
It would be really usefull, when there’s a popup window when deleting a note, so one can decide whether to delete the attachment, too.

1 Like

Please add this feature.

The feature is a must.

Useful idea

Yes this needs to be implemented