def delete_datastore(sync_job, task_metadata, task_status):
    name = store_name(sync_job)
    try:
        d_gs = gs.get_store(name)
    except:
        # datastore not exist
        return

    gs.delete(d_gs)
def delete_style(sync_job, task_metadata, task_status):
    s_gs = gs.get_style(name=sync_job["name"], workspace=sync_job["workspace"])
    if s_gs:
        gs.delete(s_gs)
def delete_feature(sync_job, task_metadata, task_status):
    l_gs = gs.get_layer("{}:{}".format(sync_job["workspace"], sync_job["name"]))
    if l_gs:
        gs.delete(l_gs)
def delete_feature(sync_job, task_metadata, task_status):
    feature_name = "{}:{}".format(sync_job["workspace"], sync_job["name"])
    l_gs = gs.get_layer(feature_name)
    styles = {}
    feature_exist = False
    # try to find associated feature's private styles
    if l_gs:
        # delete alternate styles
        feature_exist = True
        for s_gs in l_gs.styles or {}:
            if s_gs.name.startswith(sync_job["name"]):
                # the alternate style is only used by this feature, save it for delete.
                styles[s_gs.name] = s_gs

        # try to delete default style
        if l_gs.default_style and l_gs.default_style.name.startswith(sync_job["name"]):
            # has default style and default style is only used by this feature, save it for delete it.
            styles[l_gs.default_style.name] = l_gs.default_style

    # try to find feature's private styles but failed to attach to the feature
    for name, style in sync_job["styles"].iteritems():
        style_name = geoserver_style_name(sync_job, name)
        if style_name in styles:
            continue
        s_gs = gs.get_style(name=style_name, workspace=sync_job["workspace"])
        if s_gs:
            styles[style_name] = s_gs

    # delete the feature
    if l_gs:
        # delete layer
        gs.delete(l_gs)
        # delete feature
        url = (
            GEOSERVER_REST_URL
            + "workspaces/"
            + sync_job["workspace"]
            + "/datastores/"
            + store_name(sync_job)
            + "/featuretypes/"
            + sync_job["name"]
            + ".xml"
        )
        gs.delete(Feature(l_gs, url))

    # delete the styles
    for style in styles.itervalues():
        gs.delete(style)

    if feature_exist:
        if styles:
            task_status.set_message(
                "message",
                os.linesep.join(
                    [
                        "Succeed to delete feature ({})".format(feature_name),
                        "Succeed to delete private styles ({}).".format(
                            ", ".join([name for name in styles.iterkeys()])
                        ),
                    ]
                ),
            )
        else:
            task_status.set_message("message", "Succeed to delete feature ({}).".format(feature_name))
    else:
        if styles:
            task_status.set_message(
                "message",
                os.linesep.join(
                    [
                        "Feature ({}) doesn't exist.".format(feature_name),
                        "Succeed to delete private styles ({}).".format(
                            ", ".join([name for name in styles.iterkeys()])
                        ),
                    ]
                ),
            )
        else:
            task_status.set_message("message", "Feature ({}) doesn't exist.".format(feature_name))