Cannot Unflag a User Programmatically This is all programmatic: in a module.

I have a flag that I can set successfully, but I cannot unset/unflag. I do not know why, as the code seems so similar.

The flag is set in a _submit function of a form:

  // Enable the global user object
  global $user;

  //Flag the user as having MyExample credit
  // Invoke the Flag API to set the flagging. This also saves the flagging entity.
  $flag = flag_get_flag('MyExample_credited');
  $flag-&gt;flag('flag', $user-&gt;uid);


I can detect this flag successfully in other functions using this code:

if ($flag-&gt;is_flagged($user-&gt;uid)) {


However, unflagging is not working. Here is the code:

  // Enable the global user object
  global $user;

  $markup .= 'Removing credit...&lt;br&gt;';
  //Unflag whether the user has MyExample credit
  $flag = flag_get_flag('MyExample_credited');
  $markup .= 'Flag status:' . $flag-&gt;is_flagged($user-&gt;uid).'&lt;br&gt;';
  $flag-&gt;flag('unflag', $user-&gt;uid);
  $markup .= 'Flag status:' . $flag-&gt;is_flagged($user-&gt;uid).'&lt;br&gt;';


This produces the following output:


  Removing credit...
  
  Flag status:1 
  
  Flag status:1


Testing the flag on other pages also confirms that it has not been unflagged.

Why could this be? Nothing in any of the error logs. Is it to do with how I have declared the flag, perhaps?

function MyExample_flag_default_flags() {
  $flags = array();
  $flags['MyExample_credited'] = array (
    'entity_type' =&gt; 'user',
    'title' =&gt; 'MyExample Credit',
    'global' =&gt; 1,
    'types' =&gt;   array (),
    'flag_short' =&gt; 'Give credit',
    'flag_long' =&gt; 'Give this user a MyExample credit',
    'flag_message' =&gt; 'Credit given.',
    'unflag_short' =&gt; 'Remove credit',
    'unflag_long' =&gt; 'Remove a MyExample credit for this user',
    'unflag_message' =&gt; 'Credit removed.',
    'unflag_denied_text' =&gt; 'You cannot remove this credit.',
    'link_type' =&gt; 'toggle',
    'weight' =&gt; 0,
    'show_in_links' =&gt; array (
      'full' =&gt; 0,
      'heartbeat_user' =&gt; 0,
      'token' =&gt; 0,
    ),
    'show_as_field' =&gt; 0,
    'show_on_form' =&gt; 0,
    'access_author' =&gt; '',
    'show_contextual_link' =&gt; 0,
    'show_on_profile' =&gt; 0,
    'access_uid' =&gt; '',
    'module' =&gt; 'MyExample',
    'locked' =&gt; array (
      'global' =&gt; 'global',
      'status' =&gt; 'status',
    ),
    'status' =&gt; true,
    'api_version' =&gt; 3,
  );
  return $flags;
}
Could it be a permission issue?
Maybe set the "skip_permission_check" at true, as said in documentation here:
https://www.drupal.org/node/1748148#flagging