You can overwrite the entire FS using this (https://blogs.esri.com/esri/arcgis/2017/03/14/updating-your-hosted-feature-services-with-arcgis-pro-...) which is more "friendly" to error handling, or this (https://developers.arcgis.com/python/sample-notebooks/updating-features-in-a-feature-layer/) which would involve a little more complexity, and does not handle data deltas.
import sys, string, os, arcpy, calendar, datetime, traceback,smtplib
import arcpy
from arcpy import env
from arcgis.gis import GIS
from subprocess import call
NAME = "HFS_NAME"
try:
d = datetime.datetime.now()
log = open("C:\\PYTHON_LOGS\LOG."+NAME+".txt","a")
log.write("----------------------------" + "\n")
log.write("----------------------------" + "\n")
log.write("Log: " + str(d) + "\n")
log.write("\n")
starttime = datetime.datetime.now()
log.write("Begin process:\n")
log.write(" Process started at " + str(starttime) + "\n")
log.write("\n")
SERVER = "mail_server"
PORT = "25"
FROM = "sasquatch"
MAILDOMAIN = '@big.foot'
userList=["is_real"]
userNames = [u for u in userList]
emailList = [name + MAILDOMAIN for name in userNames]
TO = emailList
DATE = d
sdfilename = NAME+".sd"
sddraftfilename = NAME+".sddraft"
if os.path.exists(sdfilename): os.remove(sdfilename)
if os.path.exists(sddraftfilename):os.remove(sddraftfilename)
prjPath = r"C:\PRODUCTION\HFS_NAME.aprx"
sd_fs_name = NAME
portal = "http://www.arcgis.com"
user = "users"
password = "pass"
shrOrg = True
shrEveryone = True
shrGroups = "some group id "
relPath = sys.path[0]
sddraft = os.path.join(relPath, NAME+".sddraft")
sd = os.path.join(relPath, NAME+".sd")
print("Creating SD file")
arcpy.env.overwriteOutput = True
prj = arcpy.mp.ArcGISProject(prjPath)
mp = prj.listMaps()[0]
arcpy.mp.CreateWebLayerSDDraft(mp, sddraft, sd_fs_name, 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS','', True, True)
arcpy.StageService_server(sddraft, sd)
print("Connecting to {}".format(portal))
gis = GIS(portal, user, password)
print("Search for original SD on portal...")
sdItem = gis.content.search("title:HFS_NAME AND owner:user AND id:some sd id", item_type="Service Definition")[0]
print("Found SD: {}, ID: {} \n Uploading and overwriting...".format(sdItem.title, sdItem.id))
sdItem.update(data=sd)
print("Overwriting existing feature service...")
fs = sdItem.publish(overwrite=True)
if shrOrg or shrEveryone or shrGroups:
print("Setting sharing options...")
fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
print("Finished updating: {} - ID: {}".format(fs.title, fs.id))
os.remove(sd)
os.remove(sddraft)
endtime = datetime.datetime.now()
log.write(" Completed successfully in "
+ str(endtime - starttime) + "\n")
log.write("\n")
log.close()
print('done')
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(pymsg)
arcpy.AddError(msgs)
log.write("" + pymsg + "\n")
log.write("" + msgs + "")
log.close()
SUBJECT = "Notification of Un-Successful AGOL Update of "+NAME
MSG = "Did Not Update: {} - ID: {} at "+ str(DATE)+ "; " +pymsg + "; " + msgs
print (MSG)
print (emailList)
MESSAGE = "Subject: %s\n\n%s" % (SUBJECT, MSG)
try:
try:
print("Connecting to Server...")
server = smtplib.SMTP(SERVER,PORT)
try:
print("Login...")
try:
print("Sending mail...")
server.sendmail(FROM, TO, MESSAGE)
except Exception as e:
print("Send Error Mail\n" + e.message)
except Exception as e:
print("Error Authentication Server: check the credentials \n" + e.message)
except Exception as e:
print("Error Connecting to Server : check the URL of the server and communications port ( 25 and ' the default ) \n" + e.message)
print("Quit.")
server.quit()
except Exception as e:
print (e.message)