Is there a way to do a one-time purge and regenerate of all memberships that are shared by a relationship? (I would be okay if it required doing some SQL directly in the database as long as it didn't conflict with anything else).

The issue is that some relationships were deleted directly in the database and I want to make sure all the memberships are up-to-date.

  • 1
    Would also like to know how this can be done as I have changed the Relationship direction in our system as it was incorrect, however I need to update all memberships for this to take effect. @Laryn did you manage to get anywhere with this? Thanks.
    – NathanB
    Aug 14, 2015 at 14:08
  • 1
    @NathanB Not yet, but I'm still working it through. If no one has a suggestion off the top I suspect I'll figure something out within the week as I have time to devote to it and I'll post back here. Aug 14, 2015 at 19:30

2 Answers 2


I ran into a situation where the membership-type relationships were missing a relationship type, so I had to re-generate the related memberships.

Instead of export/import, I used the CiviCRM API:

$result = civicrm_api3('Membership', 'get', [
  'membership_type_id' => 1234, // optional
  'option.limit' => 0,

foreach ($result['values'] as $key => $val) {
  civicrm_api3('Membership', 'create', [
    'id' => $val['id'],

The Membership BAO calls createRelatedMemberships.

update: however, I had a small issue with the Membership dashboard not reflecting the numbers correctly, and had to run this query:

UPDATE civicrm_membership m
  LEFT OUTER JOIN civicrm_membership parent ON (parent.id = m.owner_membership_id)
SET m.join_date = parent.join_date, m.end_date = parent.end_date, m.start_date = parent.start_date
WHERE m.membership_type_id = 1234
  AND m.start_date is null;

I've tried this with some fake information and it seems to work in practice -- is there anything dangerous about deleting directly in the database all memberships that have an owner_membership_id that is not NULL?


  1. Delete non-primary memberships in the database (owner_membership_id is not NULL)*
  2. Export all memberships
  3. Import those same memberships without changing any information, making sure to select "Update" on the import screen and matching up the membership ID with the correct field.

Example SQL for #1 above:

FROM civicrm_membership
WHERE owner_membership_id IS NOT NULL;
  • 1
    I would also recommend checking following tables: - civicrm_membership_payment - civicrm_membership_log - If you have used price sets then civicrm_line_item Oct 28, 2015 at 7:52

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.