anchor.diff:      %% The additions are new, so we need to link them to the CBV (the
       %% checksums will already be present in the database, and the
       %% corresponding files will have already been uploaded to S3).
  -    case delete_cookbook_checksums(UnlinkFromCBV, OrgId, Id) of
  +    case unlink_checksums_from_cbv(UnlinkFromCBV, OrgId, Id) of
           ok ->
  -            DeleteFromS3 = delete_checksums(OrgId, UnlinkFromCBV),
  +            DeleteFromS3 = delete_orphaned_checksums(OrgId, UnlinkFromCBV),
               case insert_cookbook_checksums(AddToCBV, OrgId, Name, Major, Minor, Patch) of
                   ok -> {ok, AddToCBV, DeleteFromS3};
                   Error -> Error
  
positive.code:     %% that aren't in the new version's list are targeted for deletion
      UnlinkFromCBV = sets:to_list(sets:subtract(OldChecksums,
                                                 NewChecksums)),
      
      %% Conversely, checksums that are in the new list but not the old
      %% need to be added to the system
      AddToCBV = sets:to_list(sets:subtract(NewChecksums, 
                                            OldChecksums)),
  
      %% We must first dissociate the checksums-to-be-deleted from the
  
negative.code:             {error, Reason}
      end.
  
  %% @doc Delete all checksums for a given cookbook version.
  %%
  %% Returns a list of all checksums that have been completely removed
  %% from the database (because removing them from this cookbook version
  %% made them orphans); the corresponding files can now be safely
  %% removed from S3.
  -spec unlink_all_checksums_from_cbv(OrgId::object_id(),
                                      CookbookVersionId::object_id()) ->
                                             {ok, [binary()]} | {error, term()}.
  unlink_all_checksums_from_cbv(OrgId, CookbookVersionId) ->
      % retrieve a list of checksums before we delete the
      % cookbook_version_checksums record
      Checksums = fetch_cookbook_version_checksums(OrgId, CookbookVersionId),
      case sqerl:statement(delete_cookbook_checksums_by_orgid_cookbook_versionid,
                              [OrgId, CookbookVersionId]) of
          {ok, _} ->
              {ok, delete_orphaned_checksums(OrgId, Checksums)};
          {error, Error} ->
              {error, Error}
      end.
  
  %% @doc Deletes checksums from the checksums table, but only if they
  %% are not part of any existing cookbook version.
  %%
  %% Returns a list of deleted checksums (a subset of `Checksums')
  %% for further upstream processing (i.e., deletion from S3).
  -spec delete_orphaned_checksums(OrgId::binary(),
  				Checksums::[binary()]) -> [binary()].
  delete_orphaned_checksums(OrgId, Checksums) ->
      lists:foldl(fun(Checksum, Acc) ->
              case sqerl:statement(delete_checksum_by_id, [OrgId, Checksum]) of
                  {ok, N} when is_integer(N) -> %% pretend there is 1
  