CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Deleting trashed contacts with API call
Pages: [1]

Author Topic: Deleting trashed contacts with API call  (Read 971 times)

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Deleting trashed contacts with API call
February 07, 2015, 08:23:32 pm
There has been some discussion about how to skip the recycle bin on a delete call, like this.  The wiki also says the same thing about deleting contacts permanently with an API call:
Quote
Note: if you want to skip the 'recycle bin' for entities that support undelete (e.g. contacts) you should set $param['skip_undelete'] => 1);
But, when I add skip_undelete to the call, I get this error:
Quote
CiviCRM_API3_Exception: Could not delete contact in civicrm_api3() (line 42 of /var/www/web/sites/all/modules/civicrm/api/api.php).
Without this parameter, the delete call works, and moves the contact to the trash.  What is the secret to doing a permanent delete from the API?


Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Deleting trashed contacts with API call
February 08, 2015, 04:42:34 am
I have tried and the skip_undelete works for me! BUT it first told it could not because there would be financial blablabla....and after I deleted the contributions for the contact it went fine using
Code: [Select]
&id=999&skip_undelete=1
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Re: Deleting trashed contacts with API call
February 08, 2015, 06:44:23 am
Hmm.  These are spam accounts and there is nothing in them except an email and a name (a random-looking string of characters).  There shouldn't be any hangup with deleting financial data.   

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Deleting trashed contacts with API call
February 08, 2015, 06:57:45 am
As stated, it works for me  ??? Have you tried it on the demo site?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Re: Deleting trashed contacts with API call
February 08, 2015, 09:22:16 am
Ok - in case this wasn't clear, this is using PHP, not wget or anything so I can't really replicate the issue on the demo.  Code snippet is below.

Code: [Select]
function delete_spam( $template ) {
$res = civicrm_api3('Email', 'get', array(
'sequential' => 1,
'email' => array( 'LIKE' => $template ),
'is_primary' => 1,
));
// $params = array( 'skip_undelete' => 1 );
$params = array();
if (!$res['is_error']) {
foreach ( $res['values'] as $addr ) {
$params['id'] = $addr['contact_id'];
$res2 = civicrm_api3( 'Contact', 'delete', $params );
print "Deleting contact {$params['id']}\n";
}
}
}

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Deleting trashed contacts with API call
February 08, 2015, 09:38:03 am
I understand :-) You can try with the API explorer on demo?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Re: Deleting trashed contacts with API call
February 08, 2015, 02:22:36 pm
Ok I tried this on the API explorer on the demo, and successfully deleted contact #58 permanently.  I did the same thing on my test site, and it also works there through the API explorer.  So from what I can see, it only fails to work with PHP. 

Edit: I would be grateful if someone could try a permanent delete from PHP, so I know whether it is just in my install or a CiviBug.  Thanks.
« Last Edit: February 09, 2015, 10:43:02 am by robinhood »

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Re: Deleting trashed contacts with API call
February 09, 2015, 07:27:32 pm
Perhaps some progress to report.

I found that the PHP call works if the "CiviCRM: delete contacts" permission is set for anonymous users.  That is clearly not a good solution (!) but tells me that this is a permissions problem.  How does one cure this?

The wiki says:
Quote
Here's how API usage is secured. It has two modes:
  •     ignore permission (check_permissions=0): this is the default for smarty or php. This is to allow you to display information the user wouldn't see by default (eg. the list of staff to anonymous users)
  •     use permission (check_permissions=1): this is the default for REST or Ajax and it can't be overriden. This is also the mode for smarty or php if you add check_permission=1

So the default mode for PHP is to not check permissions, but this call fails on a permissions check.  Something ain't right.


Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Deleting trashed contacts with API call
February 10, 2015, 12:17:38 am
That does sound illogical  ;)
Have you tried your API call with check_permission=0?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Re: Deleting trashed contacts with API call
February 10, 2015, 04:29:03 am
Yes.  It still fails.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Deleting trashed contacts with API call
February 10, 2015, 04:59:47 am
It does not make sense if it does work with the API explorer as it will use the same basic api function with the API Explorer as it does with PHP? Is there any other influencing factor?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Re: Deleting trashed contacts with API call
February 16, 2015, 02:03:59 pm
I posted an issue.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Deleting trashed contacts with API call
February 16, 2015, 02:08:02 pm
Well the difference seems to be that when you try with the API explorer, you are logged in. Whereas with php you are trying to do it while an anonymous user is triggering your script.
Probably the contact.delete api is just not properly passing through the check_permissions flag. Can you step through the code to check and hopefully submit a patch/PR to fix the issue?
Try asking your question on the new CiviCRM help site.

robinhood

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 6
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.56
  • PHP version: 5.3.5
Re: Deleting trashed contacts with API call
February 16, 2015, 05:57:00 pm
I tried that before posting the issue, and will try again when I get some time.  Civi internal workings are a bit overwhelming.   :-[ 


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Deleting trashed contacts with API call

This forum was archived on 2017-11-26.