How to delete a huge number of Flamingo Contact Form 7 submissions

As I am working on cleaning up a web site one issue that showed up was a huge number of Flamingo submissions / Entries that came in through Contact Form 7. How would I delete these in a simple way?

I went to the Advanced WordPress group on Facebook and mentioned that I was doing a technical audit / cleaning up a web site. One of the questions I asked was how to clean up Flamingo submissions. Damien replied with a link to his web site and a blog post he had made: https://www.damiencarbery.com/2020/03/delete-contact-form-7-flamingo-submissions/

Here are two approaches Damien mentioned.
Btw Have the Flamingo submission / Entries backend WordPress page open at the same time to view how things go.

Add code to a new file which will be added to the root of the site.

Upload the file to the root of the web site and go to the url. An example. yourwebsite.com/delete-flamingo-data.php. Use any name you prefer for the file. Then click the refresh browser button a few times to delete all the submissions from Flamingo.

The code

<?php $time_start = microtime(true); ?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Delete Flamingo messages and contacts</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<h1>Delete Flamingo messages and contacts</h1>
<?php
define('WP_USE_THEMES', false);
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

$posts_to_retrieve = 200;
$args = array('post_type'=> array( 'flamingo_inbound', 'flamingo_contact', 'flamingo_outbound' ),
              'fields' => 'ids',
			  'post_status' => array( 'publish', 'trash', 'flamingo-spam' ),
              'posts_per_page' => $posts_to_retrieve,
              'no_found_rows' => true,
              'update_post_term_cache' => false,
              'update_post_meta_cache' => false);
$flamingo_msgs = new WP_Query($args);
$msgs_ids = array();
// No need for a loop because we only requested the 'id' field!
$msgs_ids = $flamingo_msgs->posts;
wp_reset_postdata();

$i = 1;  
// Display the product IDs
echo '<ul>';
foreach ( $msgs_ids as $msg_id ) {
	if ( null == wp_delete_post( $msg_id, true ) ) {
		echo "<li>Error deleting item ID: $msg_id </li>";
	}
	else {
		echo "<li>$msg_id deleted</li>";
	}
	$i++;
}
echo '</ul>';

if ( $i == $posts_to_retrieve ) {
	echo '<p>Please refresh the page as there may be more entries to delete.</p>';
}

// Display some stats - for fun.   
echo '<p>Memory usage: ', intval(memory_get_usage() / (1024 * 1024)), "MB\n";
echo '<br />Peak memory usage: ', intval(memory_get_peak_usage() / (1024 * 1024)), "MB</p>\n";

$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<p>Process Time: {$time} seconds.</p>";
?>
</body>
</html>

Add code as a new Code snippet.

Then use the shortcode [delete_flamingo_data] in a new page and save it as private so that the page does not become visible.
View the page on the frontend and refresh the browser window, until all the Flamingo submissions have been deleted.

The code

<?php
/*
Plugin Name: Delete Flamingo data shortcode
Plugin URI: https://www.damiencarbery.com/2020/03/delete-contact-form-7-flamingo-submissions/
Description: Shortcode version of standalone script that deletes Flamingo data.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1.20241013
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/


add_shortcode( 'delete_flamingo_data', 'delete_flamingo_data_shortcode' );
function delete_flamingo_data_shortcode() {
	$time_start = microtime(true);

	$posts_to_retrieve = 200;
	$args = array('post_type'=> array( 'flamingo_inbound', 'flamingo_contact', 'flamingo_outbound' ),
				  'fields' => 'ids',
				  'post_status' => array( 'publish', 'trash', 'flamingo-spam' ),
				  'posts_per_page' => $posts_to_retrieve,
				  'no_found_rows' => true,
				  'update_post_term_cache' => false,
				  'update_post_meta_cache' => false);
	$flamingo_msgs = new WP_Query($args);
	$msgs_ids = array();
	// No need for a loop because we only requested the 'id' field!
	$msgs_ids = $flamingo_msgs->posts;
	wp_reset_postdata();

	ob_start();

	$i = 1;  
	// Display the product IDs
	echo '<ul>';
	foreach ( $msgs_ids as $msg_id ) {
		if ( null == wp_delete_post( $msg_id, true ) ) {
			echo "<li>Error deleting item ID: $msg_id </li>";
		}
		else {
			echo "<li>$msg_id deleted</li>";
		}
		$i++;
	}
	echo '</ul>';

	if ( $i == $posts_to_retrieve ) {
		echo '<p>Please refresh the page as there may be more entries to delete.</p>';
	}

	// Display some stats - for fun.   
	echo '<p>Memory usage: ', intval(memory_get_usage() / (1024 * 1024)), "MB\n";
	echo '<br />Peak memory usage: ', intval(memory_get_peak_usage() / (1024 * 1024)), "MB</p>\n";

	$time_end = microtime(true);
	$time = $time_end - $time_start;
	echo "<p>Process Time: {$time} seconds.</p>";
	
	return ob_get_clean();
}
Share the article:

Leave a Reply

Your email address will not be published. Required fields are marked *