Created
May 15, 2015 11:28
-
-
Save Rudis1261/482718d242b3e9fa3ff8 to your computer and use it in GitHub Desktop.
Shell Script to Delete all Magento Contacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Delete all users from the database | |
// Can only be run from the command line | |
if (! isset($_SERVER['argv'][0])) { | |
header("HTTP/1.1 403 Unauthorized"); | |
die("Access Denied"); | |
} | |
// Otherwise make sure the user confirmed | |
if (! isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] !== "--continue") { | |
echo "WARNING!!! THIS WILL DELETE ALL CONTACTS IN MAGENTO!".PHP_EOL; | |
echo "If you are sure you want to continue add --continue and rerun script".PHP_EOL; | |
exit(1); | |
} else { | |
echo "Confirmed, running".PHP_EOL; | |
} | |
// Change to the script dir | |
chdir(dirname(__FILE__)); | |
// We may want to be able to exclude some contacts from the delete routine. | |
$exclude_customer_by_email = [ "iam@thatguy.co.za", "rudi@spree.co.za" ]; | |
// All the things we need to get this to work. | |
require_once ("../app/Mage.php"); | |
Mage::app(0); | |
umask(0); | |
Mage::register('isSecureArea', true); | |
// Get the collection of contacts without the exclusions. | |
$customers = Mage::getModel("customer/customer") | |
->getCollection() | |
->addFieldToFilter( | |
'email', | |
[ 'nin' => $exclude_customer_by_email ] | |
); | |
echo "Found (".count($customers).") Customers found, looping through and deleting".PHP_EOL; | |
echo "Start".PHP_EOL; | |
$count = 1; | |
$customer_count = count($customers); | |
// Printing out the progress of deleting the information. | |
foreach ($customers as $customer) { | |
if ($count % 25 == 0) { | |
echo "PROGRESS: ".round((($count/$customer_count) * 100), 2)."% (".$count."/".$customer_count.")".PHP_EOL; | |
} | |
$customer->delete(); | |
$count++; | |
} | |
echo "DONE".PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using a project directory example of "/var/www/project/"
To used this script
Download it
Copy it to your project directory
To execute, run the following in your Terminal
Confirm with the "--confirm" option
Optional
You can optionally define specific email addresses you would like to exclude: