Answers.Body: <p>Is this your production or test environment? </p>
  
  <p>You could try to switch off logging, save and remove the logging tables, try to delete your contribution and see if it now works. At the end switch logging back on.....</p>
  
  <p>Not sure if this is the root but potentially worth a try. (The already exists might be pointing at an entry in the log table?)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-15T11:04:12.473Z
Answers.Text: You, sir, deserve much beer. I turned logging off and contributions can now be updated and deleted. Is this an issue with the Civi core?
Answers.UserDisplayName: Rich Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-15T12:17:22.273Z
Answers.Text: I do not know, I have not seen this one before :-) Have seen cases of custom fields being added programmatically which are then not in the log tables and then throw up errors. Glad it worked, you could have been searching for hours....
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-16T15:25:21.030Z
Answers.Text: @RichSmith It looks like a data issue - you're deleting an activity that the logs believe was already deleted.  Is it possible you restored a database from backup, but not the corresponding log file?  That could cause this.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-26T16:58:13.040Z
Answers.Text: Hi @jon-g-megaphone-tech. I've not restored the main database at all - not sure how the main database tables and the logging tables could have got out of sync. Interestingly, today I came across a similar issue on a completely different instance of CiviCRM - different server and a different version of Civi. This time, contribution batch entries couldn't be processed because of the logging table failing with the error "1034 ** Incorrect key file for table 'log_civicrm_contact'; try to repair it". Turning logging off allowed the batch entries to be processed.
Answers.UserDisplayName: Rich Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-15T10:49:29.970Z
Answers.LastActivityDate: 2019-07-15T10:49:29.970Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>So, I can't delete or update contributions from CiviCRM 5.13 or 5.15, either through the UI or the API (v3).</p>
  
  <p>This is the backtrace:</p>
  
  <p>{</p>
  
  <pre><code>"error_code": "already exists",
  "sql": "DELETE FROM civicrm_activity WHERE source_record_id IN (78772) AND activity_type_id IN ( 6 ) [nativecode=1062 ** Duplicate entry '78823' for key 'id']",
  "trace": "#0 /var/www/html/dc/sites/all/modules/civicrm/vendor/pear/pear-core-minimal/src/PEAR.php(922): CRM_Core_Error::exceptionHandler(Object(DB_Error))\n#1 /var/www/html/dc/sites/all/modules/civicrm/packages/DB.php(985): PEAR_Error-&gt;__construct('DB Error: alrea...', -5, 16, Array, 'DELETE FROM civ...')\n#2 /var/www/html/dc/sites/all/modules/civicrm/vendor/pear/pear-core-minimal/src/PEAR.php(575): DB_Error-&gt;__construct(-5, 16, Array, 'DELETE FROM civ...')\n#3 /var/www/html/dc/sites/all/modules/civicrm/vendor/pear/pear-core-minimal/src/PEAR.php(223): PEAR-&gt;_raiseError(Object(DB_mysqli), NULL, -5, 16, Array, 'DELETE FROM civ...', 'DB_Error', true)\n#4 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/common.php(1907): PEAR-&gt;__call('raiseError', Array)\n#5 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/mysqli.php(933): DB_common-&gt;raiseError(-5, NULL, NULL, 'DELETE FROM civ...', '1062 ** Duplica...')\n#6 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/mysqli.php(403): DB_mysqli-&gt;mysqliRaiseError()\n#7 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/common.php(1216): DB_mysqli-&gt;simpleQuery('DELETE FROM civ...')\n#8 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/DataObject.php(2415): DB_common-&gt;query('DELETE FROM civ...')\n#9 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/DataObject.php(1607): DB_DataObject-&gt;_query('DELETE FROM civ...')\n#10 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/DAO.php(439): DB_DataObject-&gt;query('DELETE FROM civ...')\n#11 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/DAO.php(1414): CRM_Core_DAO-&gt;query('DELETE FROM civ...', true)\n#12 /var/www/html/dc/sites/all/modules/civicrm/CRM/Activity/BAO/Activity.php(186): CRM_Core_DAO::executeQuery('DELETE FROM civ...')\n#13 /var/www/html/dc/sites/all/modules/civicrm/CRM/Contribute/BAO/Contribution.php(1322): CRM_Activity_BAO_Activity::deleteActivity(Array)\n#14 /var/www/html/dc/sites/all/modules/civicrm/api/v3/Contribution.php(234): CRM_Contribute_BAO_Contribution::deleteContribution(78772)\n#15 /var/www/html/dc/sites/all/modules/civicrm/Civi/API/Provider/MagicFunctionProvider.php(101): civicrm_api3_contribution_delete(Array)\n#16 /var/www/html/dc/sites/all/modules/civicrm/Civi/API/Kernel.php(168): Civi\\API\\Provider\\MagicFunctionProvider-&gt;invoke(Array)\n#17 /var/www/html/dc/sites/all/modules/civicrm/Civi/API/Kernel.php(99): Civi\\API\\Kernel-&gt;runRequest(Array)\n#18 /var/www/html/dc/sites/all/modules/civicrm/api/api.php(23): Civi\\API\\Kernel-&gt;runSafe('Contribution', 'delete', Array, NULL)\n#19 /var/www/html/dc/sites/all/modules/civicrm/CRM/Utils/REST.php(316): civicrm_api('Contribution', 'delete', Array)\n#20 /var/www/html/dc/sites/all/modules/civicrm/CRM/Utils/REST.php(566): CRM_Utils_REST::process(Array, Array)\n#21 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/Invoke.php(277): CRM_Utils_REST::ajax()\n#22 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/Invoke.php(85): CRM_Core_Invoke::runItem(Array)\n#23 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/Invoke.php(52): CRM_Core_Invoke::_invoke(Array)\n#24 /var/www/html/dc/sites/all/modules/civicrm/drupal/civicrm.module(444): CRM_Core_Invoke::invoke(Array)\n#25 /var/www/html/dc/includes/menu.inc(527): civicrm_invoke('ajax', 'rest')\n#26 /var/www/html/dc/index.php(21): menu_execute_active_handler()\n#27 {main}",
  "is_error": 1,
  "error_message": "DB Error: already exists",
  "debug_information": "DELETE FROM civicrm_activity WHERE source_record_id IN (78772) AND activity_type_id IN ( 6 ) [nativecode=1062 ** Duplicate entry '78823' for key 'id']"
  </code></pre>
  
  <p>}</p>
  
  <p>Running the same SQL query mentioned in the backtrace in phpMyAdmin returns a single record (not sure why it's returning a Duplicate entry error in the backtrace).</p>
  
  <p>I've got a backup of the same system running on version 5.11, separately but on the same server, which is completely fine - no issues at all. Tried turning off various extensions but to no avail. Fairly sure that it's to do with the server configuration, but really struggling to pin down where the issue lies.</p>
  
  <p>Any advice would be very welcome. Thanks.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-15T08:22:30.543Z
Comments.Text: Do you have logging switched on?
Comments.UserDisplayName: ErikH - CiviCooP
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-15T10:20:14.677Z
Comments.Text: I do. The log entries related to the error look largely the same as the backtrace; it seems to be some issue with CiviActivity:{#2 /var/www/html/dc/sites/all/modules/civicrm/packages/DB.php(985): PEAR_Error->__construct("DB Error: already exists", -5, 16, (Array:2), "DELETE FROM civicrm_activity WHERE source_record_id IN (78772) AND activity_t...")}
Comments.UserDisplayName: Rich Smith
Tags: <contributions><activity>
Title: Can't delete or update contributions in CiviCRM 5.13+
ThreadText: # Can't delete or update contributions in CiviCRM 5.13+
  
  (Asked by: username_0 on 2019-07-15)
  
  So, I can't delete or update contributions from CiviCRM 5.13 or 5.15, either through the UI or the API (v3).
  This is the backtrace:
  {
  "error_code": "already exists",
  "sql": "DELETE FROM civicrm_activity WHERE source_record_id IN (78772) AND activity_type_id IN ( 6 ) [nativecode=1062 ** Duplicate entry '78823' for key 'id']",
  "trace": "#0 /var/www/html/dc/sites/all/modules/civicrm/vendor/pear/pear-core-minimal/src/PEAR.php(922): CRM_Core_Error::exceptionHandler(Object(DB_Error))\n#1 /var/www/html/dc/sites/all/modules/civicrm/packages/DB.php(985): PEAR_Error->__construct('DB Error: alrea...', -5, 16, Array, 'DELETE FROM civ...')\n#2 /var/www/html/dc/sites/all/modules/civicrm/vendor/pear/pear-core-minimal/src/PEAR.php(575): DB_Error->__construct(-5, 16, Array, 'DELETE FROM civ...')\n#3 /var/www/html/dc/sites/all/modules/civicrm/vendor/pear/pear-core-minimal/src/PEAR.php(223): PEAR->_raiseError(Object(DB_mysqli), NULL, -5, 16, Array, 'DELETE FROM civ...', 'DB_Error', true)\n#4 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/common.php(1907): PEAR->__call('raiseError', Array)\n#5 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/mysqli.php(933): DB_common->raiseError(-5, NULL, NULL, 'DELETE FROM civ...', '1062 ** Duplica...')\n#6 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/mysqli.php(403): DB_mysqli->mysqliRaiseError()\n#7 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/common.php(1216): DB_mysqli->simpleQuery('DELETE FROM civ...')\n#8 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/DataObject.php(2415): DB_common->query('DELETE FROM civ...')\n#9 /var/www/html/dc/sites/all/modules/civicrm/packages/DB/DataObject.php(1607): DB_DataObject->_query('DELETE FROM civ...')\n#10 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/DAO.php(439): DB_DataObject->query('DELETE FROM civ...')\n#11 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/DAO.php(1414): CRM_Core_DAO->query('DELETE FROM civ...', true)\n#12 /var/www/html/dc/sites/all/modules/civicrm/CRM/Activity/BAO/Activity.php(186): CRM_Core_DAO::executeQuery('DELETE FROM civ...')\n#13 /var/www/html/dc/sites/all/modules/civicrm/CRM/Contribute/BAO/Contribution.php(1322): CRM_Activity_BAO_Activity::deleteActivity(Array)\n#14 /var/www/html/dc/sites/all/modules/civicrm/api/v3/Contribution.php(234): CRM_Contribute_BAO_Contribution::deleteContribution(78772)\n#15 /var/www/html/dc/sites/all/modules/civicrm/Civi/API/Provider/MagicFunctionProvider.php(101): civicrm_api3_contribution_delete(Array)\n#16 /var/www/html/dc/sites/all/modules/civicrm/Civi/API/Kernel.php(168): Civi\\API\\Provider\\MagicFunctionProvider->invoke(Array)\n#17 /var/www/html/dc/sites/all/modules/civicrm/Civi/API/Kernel.php(99): Civi\\API\\Kernel->runRequest(Array)\n#18 /var/www/html/dc/sites/all/modules/civicrm/api/api.php(23): Civi\\API\\Kernel->runSafe('Contribution', 'delete', Array, NULL)\n#19 /var/www/html/dc/sites/all/modules/civicrm/CRM/Utils/REST.php(316): civicrm_api('Contribution', 'delete', Array)\n#20 /var/www/html/dc/sites/all/modules/civicrm/CRM/Utils/REST.php(566): CRM_Utils_REST::process(Array, Array)\n#21 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/Invoke.php(277): CRM_Utils_REST::ajax()\n#22 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/Invoke.php(85): CRM_Core_Invoke::runItem(Array)\n#23 /var/www/html/dc/sites/all/modules/civicrm/CRM/Core/Invoke.php(52): CRM_Core_Invoke::_invoke(Array)\n#24 /var/www/html/dc/sites/all/modules/civicrm/drupal/civicrm.module(444): CRM_Core_Invoke::invoke(Array)\n#25 /var/www/html/dc/includes/menu.inc(527): civicrm_invoke('ajax', 'rest')\n#26 /var/www/html/dc/index.php(21): menu_execute_active_handler()\n#27 {main}",
  "is_error": 1,
  "error_message": "DB Error: already exists",
  "debug_information": "DELETE FROM civicrm_activity WHERE source_record_id IN (78772) AND activity_type_id IN ( 6 ) [nativecode=1062 ** Duplicate entry '78823' for key 'id']"
  
  }
  Running the same SQL query mentioned in the backtrace in phpMyAdmin returns a single record (not sure why it's returning a Duplicate entry error in the backtrace).
  I've got a backup of the same system running on version 5.11, separately but on the same server, which is completely fine - no issues at all. Tried turning off various extensions but to no avail. Fairly sure that it's to do with the server configuration, but really struggling to pin down where the issue lies.
  Any advice would be very welcome. Thanks.
  
  **Tags:** contributions, activity
  
  > **Comments:**
  > **username_1**: Do you have logging switched on?  
  > **username_0**: I do. The log entries related to the error look largely the same as the backtrace; it seems to be some issue with CiviActivity:{#2 /var/www/html/dc/sites/all/modules/civicrm/packages/DB.php(985): PEAR_Error->__construct("DB Error: already exists", -5, 16, (Array:2), "DELETE FROM civicrm_activity WHERE source_record_id IN (78772) AND activity_t...")}  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-07-15. Score: 4 | Accepted answer
  
  Is this your production or test environment?
  
  You could try to switch off logging, save and remove the logging tables, try to delete your contribution and see if it now works. At the end switch logging back on.....
  
  Not sure if this is the root but potentially worth a try. (The already exists might be pointing at an entry in the log table?)
  
  > **Comments:**
  > **username_0**: You, sir, deserve much beer. I turned logging off and contributions can now be updated and deleted. Is this an issue with the Civi core?  
  > **username_1**: I do not know, I have not seen this one before :-) Have seen cases of custom fields being added programmatically which are then not in the log tables and then throw up errors. Glad it worked, you could have been searching for hours....  
  > **username_2**: @RichSmith It looks like a data issue - you're deleting an activity that the logs believe was already deleted.  Is it possible you restored a database from backup, but not the corresponding log file?  That could cause this.  
  > **username_0**: Hi @jon-g-megaphone-tech. I've not restored the main database at all - not sure how the main database tables and the logging tables could have got out of sync. Interestingly, today I came across a similar issue on a completely different instance of CiviCRM - different server and a different version of Civi. This time, contribution batch entries couldn't be processed because of the logging table failing with the error "1034 ** Incorrect key file for table 'log_civicrm_contact'; try to repair it". Turning logging off allowed the batch entries to be processed.  
  
  ---
  
  