Easily delete a large number of Flamingo (Contact Form 7) messages.
I recently began work on a website revamp for a client. While reviewing the current site I noticed that the CF7 Flamingo plugin was active and had a over 2000 stored submissions! I didn’t want to delete them manually through the Dashboard.
I did a web search and a number of sites recommended using a single SQL command in phpMyAdmin:
DELETE FROM `wp_posts` WHERE `post_status` LIKE 'flamingo_spam'
Unfortunately this leaves a number of records in the wp_postmeta table (8 rows for each message). Furthermore, it doesn’t delete the address book or non-spam entries.
I decided to write a standalone script to use the WordPress API to delete each post, knowing that it would also delete the corresponding postmeta records.
After writing the code I found a post in a Flamingo support thread that mentioned the post meta.
The address book
Flamingo also stores the email addresses corresponding to contact form submissions. These are stored with a different post type – ‘flamingo_contact‘ – so this needed to be added to the query’s post type.
Then I browsed the Flamingo code and found the ‘flamingo_outbound‘ post type and included it too.
Now we’re all set.
I decided to limit the number of records returned to 200, in case the deletion process timed out. I just had to reload the script to delete the next batch of records.
You will also see that I used the ‘fields‘ parameter so that I get all the post IDs without having to do a loop. It’s not really necessary here because I have to run a loop to call wp_delete_post() but it would simplify other scenarios where you didn’t need to access any other post info.
The time and memory stats are included just for fun.