Is there a wp-cli command or any easy way to remove files that are unrelated to WordPress, but FTPed or synced to the uploads directory accidentally?


I don't know whether there is a better WP-CLI way or not, but you could write a simple script that recursively crawls your uploads directory (or it's subdirectories) and than check the absolute paths (URLs) of those files and compare them against guid column in wp_posts table.

If your file is not there, WordPress doesn't know about it and it probably can be deleted.

  • Danger, danger. Files can not only be linked in posts. Couldn't also a widget or even a category contain a link to an unmanaged file? Probably worth crawling your site after the purge to find any 404 caused by missing files. – leymannx Aug 11 '20 at 7:55
  • 1
    @leymannx I believe it doesn't metter whether the image is added to a post, to a widget or to something else. Because once the image gets upload via native WP uploader, it is stored in the database as a post, which is WP's default entity. It is stored in wp_post table with post_type set to attachment (and post_mime_type set to image/png). You don't have to crawl anything, only compare your /uploads/ folder with wp_posts table. – Kristián Filo Aug 18 '20 at 8:15
  • Ah yeah, but I meant those files that have been uploaded via FTP and just a link has been put in the post/widget/whatsoever. Unmanaged files. – leymannx Aug 18 '20 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.