code: func (s *Storage) removeLeastRecent(name string, max int) error {
  	if max < 0 {
  		return nil
  	}
  	h, err := s.History(name)
  	if err != nil {
  		return err
  	}
  	if len(h) <= max {
  		return nil
  	}
  
  	// We want oldest to newest
  	relutil.SortByRevision(h)
  
  	lastDeployed, err := s.Deployed(name)
  	if err != nil {
  		return err
  	}
  
  	var toDelete []*rspb.Release
  	for _, rel := range h {
  		// once we have enough releases to delete to reach the max, stop
  		if len(h)-len(toDelete) == max {
  			break
  		}
  		if lastDeployed != nil {
  			if rel.GetVersion() != lastDeployed.GetVersion() {
  				toDelete = append(toDelete, rel)
  			}
  		} else {
  			toDelete = append(toDelete, rel)
  		}
  	}
  
  	// Delete as many as possible. In the case of API throughput limitations,
  	// multiple invocations of this function will eventually delete them all.
  	errors := []error{}
  	for _, rel := range toDelete {
  		err = s.deleteReleaseVersion(name, rel.GetVersion())
  		if err != nil {
  			errors = append(errors, err)
  		}
  	}
  
  	s.Log("Pruned %d record(s) from %s with %d error(s)", len(toDelete), name, len(errors))
  	switch c := len(errors); c {
  	case 0:
  		return nil
  	case 1:
  		return errors[0]
  	default:
  		return fmt.Errorf("encountered %d deletion errors. First is: %s", c, errors[0])
  	}
  }
docs: // removeLeastRecent removes items from history until the length number of releases
  // does not exceed max.
  //
  // We allow max to be set explicitly so that calling functions can "make space"
  // for the new records they are going to write.