How to prevent options.php deleting valid existing data from the database

Your sanitize_callback function should not return false or null indicating that input is not valid – its intent is to always return a valid value. The value returned by this function is what will be save to the database, so in order for it to actually sanitize anything it will have to return something that makes sense. Compare it with using intval as in this example, which will force the settings API to store an int value no matter what the input.

Anyway, to solve you problem your should:

  • Return the previously stored value if the input is invalid
  • Return a default value if input is empty (and empty values is not permitted)
  • Return the newly input value if it is deemed valid

Also, in addition to the last statement, your value might well become valid during sanitation: imagine inputs such as " [email protected]" or "0123" which might get be sanitized to "[email protected]" and 123 depending on your needs.