Keeping Your Obsidian Vault Tidy: Detecting and Listing Orphaned Images with Obsidian Bases

I wish I would have seen this post before making my own script in dataviewjs :stuck_out_tongue:
Didn’t take long but here it is for the curious:

const imgSize = 350
const validExtensions = ["md", "pdf", "py"]

const pages = dv.pages()
const images = app.vault.getFiles().filter(file => !validExtensions.contains(file.extension))
var outlinks = []

pages.forEach((dfile) => {
	var fileOutlinks = dfile.file.outlinks.array()
	fileOutlinks.forEach((link) => {
		if (!outlinks.includes(link)) {
			outlinks.push(link.path)
		}
	})
})

const badImages = images.filter(image => {
	return !outlinks.includes(image.path)
})

dv.table(["path", "image"], badImages.map(img => {
	const imgPath = `<img src="${img.path}" style="max-width: ${imgSize}px; max-height: ${imgSize}px;"><hr>`
	const arr = [`[[${img.path}]]`, imgPath]
	return arr
}))

There’s definitely a more elegant way to do it besides querying the entire vault twice. But eh, it probably works as long as you don’t have 8,322 orphaned images lol

2 Likes

You can double check by opening the image in Obsidian and look at the incoming links:

If it says _No backlinks found_ then Obsidian considers it orphaned

Hi, this looks great. thank you!
does this able to filter the folder/file name that we don’t want to include in the orphaned bases view? I have some folder that are gitignored and therefore some of my images that should be attached on those files are included to the orphaned view

Sure, you would add filters like this:

1 Like

tahnk you!

Thanks @WetHat for this and thanks for replying to my post about a “bug“ or “limitation” in Bases for failing to see links in images which are only imported in Bases (post here). I have downloaded and used your Bases file, and unfortunately, has the same “bug“ or “limitation” that I mentioned in my post.

See below two screenshots, of my Bases file and your Bases file, they show share the same problem. Please try the following on your side and please let me know the result:

  1. Create an empty Canvas and give it a name

  2. Paste an image, anything

  3. See what your Bases file says –> My guess is that it will show up as an orphan, while clearly it is not, because it is being used in a Canvas file of yours

(your Bases file)

(my Bases file)

I wonder, after listing more than 8000 orphant images, what will you do next? :rofl: Writting a script to move them all into the trash?

1 Like