3

How can I delete a contact through the command line?

5

If you're on Drupal and have drush installed you can do

drush cvapi Contact.delete id=17 skip_undelete=1
4

On the command line, type php path/to/civicrm/bin/cli.php -e Contact -a delete --output --contact_id=17 (Note: passing a contact_id is mandatory when deleting. But you can delete multiple contacts by creating a php file that has a loop and executes the above command through exec().)

If you want to permanently delete a contact, add the skip_undelete parameter as well: php path/to/civicrm/bin/cli.php -e Contact -a delete --output --contact_id=17 --skip_undelete=true

I was, however, having problems with permanent deleting. I was just getting the error Could not delete contact. Looking at the code, my suspicion proved right - it was a permissions issue. So I tried to pass my admin CMS user login credentials: php path/to/civicrm/bin/cli.php -e Contact -a delete -u myuser -p mypass --output --contact_id=17 --skip_undelete=true. Unfortunately, for some reason it didn't work for me. Anyways, I thought I'd share my experiences - maybe I'm missing something or it might work for someone else. To circumvent this, in my case I just temporarily commented out the permissions check in CRM_Contact_BAO_Contact->deleteContact().

Tip: because the delete isn't very fast, if you are deleting a large number of contacts it might take quite a while to go through them. The little trick I used to speed this up was to start multiple instances of my shell and have each of them delete a portion of my contacts. In that case I found it more convenient to use a bash for loop, like so: for i in {50000..100000}; do php cli.php -e Contact -a delete --output --contact_id=$i --skip_undelete=true; echo Deleted contact_id $i; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.