Skip to content

Instantly share code, notes, and snippets.

@Rudis1261
Created May 15, 2015 11:28
Show Gist options
  • Save Rudis1261/482718d242b3e9fa3ff8 to your computer and use it in GitHub Desktop.
Save Rudis1261/482718d242b3e9fa3ff8 to your computer and use it in GitHub Desktop.
Shell Script to Delete all Magento Contacts
<?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;
@Rudis1261
Copy link
Author

Using a project directory example of "/var/www/project/"

To used this script

Download it

wget -O ~/delete_all_contacts.php http://git.io/vUbss

Copy it to your project directory

cp ~/delete_all_contacts.php /var/www/project/htdocs/shell

To execute, run the following in your Terminal

php /var/www/project/htdocs/shell/delete_all_contacts.php

Confirm with the "--confirm" option

Optional

You can optionally define specific email addresses you would like to exclude:

$exclude_customer_by_email = [ "iam@thatguy.co.za", "rudi@spree.co.za" ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment