I have 300-400 CiviCRM smart groups I want to delete. I will know the id for the group but need to know what other tables I need to delete that may be related.

Also, is there any way to identify which groups are smart groups?

Here is some code I have tried.

<?php require_once 'api/class.api.php'; $api = new civicrm_api3(); $result = $api->group->delete ( $id=1544 ); ?>

Here is the result:

PHP Fatal error: Class 'CRM_Core_Config' not found in /home/daweb_dev/democratsabroad/drupal/sites/all/modules/civicrm/api/class.api.php on line 92 PHP Stack trace: PHP 1. {main}() /home/daweb_dev/democratsabroad/drupal/sites/all/modules/civicrm/delgroup.php:0 PHP 2. civicrm_api3->__construct() /home/daweb_dev/democratsabroad/drupal/sites/all/modules/civicrm/delgroup.php:3

This worked using rest.php

wget -a delgroup.log -O delgroup.out 'https://www.example.com/sites/all/modules/civicrm/extern/rest.php?entity=group&action=delete&key=site-key&api_key=api-key&version=3&json=1&id=923'


I suggest you use the api.

Visiting yoursite.org/civicrm/api will let you try it out.

  • That link just takes me to the dashboard
    – Vietyank
    Oct 10 '15 at 3:33
  • Maybe you are using an older version of Civi. Try yoursite.org/civicrm/api/explorer
    – Coleman
    Oct 10 '15 at 15:50
  • That worked but I have never written any php using the API. What I've got is a spreadsheet with a list of group ids I want to delete. There are about 400 of them so I really want to write a script. It looks like the API will do the trick and remove members from the groupas well but I need to find an example of how to write the PHP. This is CiviCRM 4.1.5. We are migrating to Nation Builder and need to delete these groups before the migration.
    – Vietyank
    Oct 11 '15 at 4:09
  • Sorry...I cannot figure out how to put code in the comments
    – Vietyank
    Oct 11 '15 at 6:11
  • 1
    Sorry to lose you to NB - enjoy their new higher prices.
    – Coleman
    Oct 13 '15 at 15:26

The challenge seems to be instantiating civicrm within a script - the easiest ways are

1) through the api explorer 2) through drush - ie drush eval"civicrm_initialize(); script lines here" 3) through devel in the UI looks much like drush

Otherwise for a standalone php script (that doesn't leverage drupal) you probably want to copy the way one of the scripts in the bin directory does it - but that way is more painful :-(

  • This has to be run on a production system that does not have Drush installed, otherwise I could do it with Drush. I have seen other posts that other people had problems with CiviCRM initialize too
    – Vietyank
    Oct 12 '15 at 0:43
  • Haha. I was looking for something not too painful for a newb.
    – Vietyank
    Oct 12 '15 at 0:44
  • I'd still like to know how to do it with a standalone php script using the API
    – Vietyank
    Oct 12 '15 at 7:19
  • While Eileen's answer was informative, I still was not able to write a php script using the API directly.
    – Vietyank
    Oct 14 '15 at 5:08

You could also use chaining in the api explorer ie.

civicrm_api3('Group', 'get', array('id' => array('IN' => array(4,6,7,8,9), 'api.group.delete');

But test thoroughly on dev first!!!

  • if that is the 'solution' then suggest you add as an answer ;-)
    – petednz - fuzion
    Oct 12 '15 at 19:46

I got it to work by calling rest.php with wget wget -a delgroup.log -O delgroup.out 'example.com/sites/all/modules/civicrm/extern/…;

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.