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.
Incomplete Method
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 query
The suggested SQL command was helpful in providing the post type to query for (‘flamingo_spam‘). Then I would call wp_delete_post() to completely delete the post (by passing trash).
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.
The code
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.
Nice work! I wonder how difficult it would be to delete ALL (not just spam) messages in CF7 / Flamingo after a certain period (e.g. 30 days) for a certain contactform (e.g. registration-form-1). This would allow a proper process for GDPR purposes differentiated to certain information and would be very useful.
Does anybody here could adjust the code to that goal?
Thanks
@Elenora: The quickest solution is to change to use Ninja Forms :-) It has this feature built in. It is also lighter than CF7 which is a bit of a hog.
If you stick with CF7 you’d need a cron job that would run a query for all CF7 submissions older than 30 days and delete them. I don’t think that it would be particularly difficult. I might write such code if I get some time between projects (I’m very busy at the moment).
Lifesaver. Thank you!
Hi, I’m a not very knowledgeable on PHP, but need to clean-up our Flamingo DB. Can you explain what to do with your script?
@Wayne: Download the file and upload it to the root directory of your website. Then go to the page e.g. https://yoursite.com/delete-flamingo-data.php Keep reloading the page until there are no entries left.
Delete the file when you are finished.
Hey, how do I use the code? Just create a php file, upload it and run it?
@Rubb: Download the file and upload it to the root directory of your website. Then go to the page e.g. https://yoursite.com/delete-flamingo-data.php Keep reloading the page until there are no entries left.
Delete the file when you are finished.