How can I bulk delete media and attachments using WP-CLI?

The question:

I’m trying to mass delete 4000 images in a wordpress website. WP itself sets the max to 999, which would work fine and take a few minutes to delete them via the backend. However, I get REQUEST URI TOO LARGE because the backend form uses GET instead of POST.

How can I use WP-CLI to perform the operation if it’s possible and ensure the integrity of the WP database. In other words, I don’t want to run an SQL command.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

From the WP-CLI documentation about wp post delete:

wp post delete --force $(wp post list --post_type='attachment' --format=ids)

See wp post list for additional information.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment