fix update to remove shares where file doesn't exist on postgres

This is just a cherry pick of the changes from #6016 to be able to add unit tests to this PR.

credit for the fix goes to @jmcclelland