How can I batch delete all unattached images with WP-CLI or other automated process?

You can try this (untested) modification to the answer you linked to by @something

wp post delete $(wp post list --post_type="attachment" --format=ids --post_parent=0)

to delete attachments without parents.

To target a given mime type, e.g. image/jpeg try:

wp post delete $(wp post list --post_type="attachment" \
    --format=ids --post_parent=0 --post_mime_type="image/jpeg")

Notes:

  • Remember to backup first before testing!

  • Unattached images might still be used in the post content or e.g. in widgets.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)