Skip to content

Delete a department

This endpoint removes a department from the account. It takes no request body; the department is identified by the path parameter, and the caller must hold the departments:write scope. The request either completes synchronously with 204 No Content or returns 202 Accepted with an operation object to poll. Two conditions block deletion outright: the department is the company's default, or it still has sub-departments.

Request at a glance

Method: DELETE. Path: /v2/departments/{id}. Required scope: departments:write. No request body. The acting user is the API key owner, or the user named in the X-On-Behalf-Of header when that scope is present. The endpoint waits for the deletion and responds with 204 when it completes, or 202 Accepted with an operation to poll when the work is still running.

Authorization

Send the API key as Authorization: Bearer <token>. The key must carry departments:write. Key creation, the full scope list, and how the key owner's role limits the key are covered in the authentication and scopes documentation. The scope is declared in the header, not in the path or body.

Path parameter: id

The only path parameter is id, a required string identifying the department to delete. Supply the resource ID exactly as returned by the department read endpoints; the API does not accept a name or slug in its place.

Optional headers

Two optional headers change how the request behaves.

Idempotency-Key makes the write safe to retry. It accepts 1 to 255 printable ASCII characters and is retained for 24 hours per API key. Within that window, repeating the same key with the same request replays the original response and adds the Idempotent-Replayed header. Three failure modes apply: pairing the key with a different request yields 422 IDEMPOTENCY_KEY_REUSED; sending a retry while the first request is still running yields 409 IDEMPOTENCY_REQUEST_IN_PROGRESS; and a key that breaks the length or character rule yields 400 INVALID_IDEMPOTENCY_KEY.

X-On-Behalf-Of names the user the write is performed for. Two conditions must both hold: the key carries the users:act-on-behalf scope, and the key owner manages the target user. If either fails, the request returns 403 ON_BEHALF_OF_NOT_ALLOWED. The header has no effect on GET requests.

Pre-flight checklist

Before sending the DELETE, confirm each of the following. If any answer is no, fix the condition first rather than retrying the request.

  • Is the target department not the company's default? If it is the default, make another department the default first; the default can never be deleted.
  • Does the department have zero sub-departments? If it has any, delete or move them first.
  • Does the API key hold departments:write?
  • If using X-On-Behalf-Of, does the key also hold users:act-on-behalf, and does the key owner manage the target user?
  • If retrying, is the same Idempotency-Key paired with the same request?
  • Is the id the actual resource ID, and within the key's data reach?

Blocking conditions and how to clear them

Two conditions return 409 and must be resolved before the delete can succeed. DEFAULT_DEPARTMENT_DELETE_FORBIDDEN means the department is the company default; promote another department to default, then retry. DEPARTMENT_HAS_SUBDEPARTMENTS means the department still has children; delete or move each sub-department, then retry. Both are state conflicts, not permission errors, so adding scopes will not help.

Response handling

204 No Content means the deletion completed and there is nothing to read back. 202 Accepted returns an application/json body containing an operation object with id, status, and url; the Location header carries the same URL as operation.url. Poll that URL until status is SUCCEEDED or FAILED. A status of UNKNOWN means no result arrived within 15 minutes; the change may or may not have been applied, so read the resource to check. A result that arrives later still moves the status to SUCCEEDED or FAILED.

Distinguishing the failure responses

403 FORBIDDEN means the acting user may not perform the action. 403 ON_BEHALF_OF_NOT_ALLOWED means the X-On-Behalf-Of header was used without the users:act-on-behalf scope, or the key owner does not manage the target user. 404 RESOURCE_NOT_FOUND means the department does not exist or is outside the key's data reach; the two cases are deliberately indistinguishable, so check both the ID and the key's visibility before assuming the department is gone. 409 covers the two blocking conflicts above plus the Idempotency-Key codes. 422 IDEMPOTENCY_KEY_REUSED means the same key was paired with a different request. 400 INVALID_IDEMPOTENCY_KEY means the key itself failed the length or character rule. 429 RATE_LIMITED means too many requests were sent in a short time; wait and retry with the same Idempotency-Key. 500 INTERNAL_ERROR means a server-side failure; retry later and quote the requestId if it persists.

Worked example

Suppose the target department is not the default and has no sub-departments, and the key holds departments:write. Send DELETE /v2/departments/{id} with Authorization: Bearer <token> and an Idempotency-Key. If the API returns 202, the body looks like the operation object below, and the client should poll operation.url until status is SUCCEEDED or FAILED. If the API returns 204, the deletion is complete and no further call is needed. If the API returns 409 DEPARTMENT_HAS_SUBDEPARTMENTS, list the department's children, delete or move them, then retry with the same Idempotency-Key only if the request is otherwise unchanged.

Inputs and controls
FieldLocationRequiredNotes
idpathyesresource ID (string)
AuthorizationheaderyesBearer <token>; key must hold departments:write
Idempotency-Keyheaderno1-255 printable ASCII; retained 24h per API key; replays original response with Idempotent-Replayed
X-On-Behalf-Ofheadernouser ID; requires users:act-on-behalf and key owner manages target user; ignored on GET

Header values and scope names are exact contract strings; do not alter case.

Response statuses and meaning
StatusMeaningWhat to do
202Accepted; operation object returned with id, status, urlPoll operation.url until SUCCEEDED or FAILED; UNKNOWN means read the resource to check
204No Content; deletion completedNothing further
400INVALID_IDEMPOTENCY_KEYFix the Idempotency-Key format
401UNAUTHORIZEDSend a valid vt_live_ key in Authorization: Bearer
403FORBIDDEN or ON_BEHALF_OF_NOT_ALLOWEDCheck acting user permissions and X-On-Behalf-Of scope
404RESOURCE_NOT_FOUNDCheck the id and the key's data reach
409DEFAULT_DEPARTMENT_DELETE_FORBIDDEN, DEPARTMENT_HAS_SUBDEPARTMENTS, or IDEMPOTENCY_REQUEST_IN_PROGRESSResolve the state conflict or wait for the in-flight request
422IDEMPOTENCY_KEY_REUSEDUse a new key for a different request
429RATE_LIMITEDWait and retry with the same Idempotency-Key
500INTERNAL_ERRORRetry later; quote requestId if it persists

Status codes and error codes are exact contract strings. The 202 operation status enum is PENDING | SUCCEEDED | FAILED | UNKNOWN.

Failure distinction: 403 vs 404 vs 409
SymptomLikely causeFix
403 FORBIDDENActing user lacks permissionUse a key owner or acting user with the needed role
403 ON_BEHALF_OF_NOT_ALLOWEDX-On-Behalf-Of used without users:act-on-behalf, or key owner does not manage target userAdd the scope or choose a managed target user
404 RESOURCE_NOT_FOUNDDepartment does not exist, or is outside the key's data reachVerify the id and the key's visibility; the two cases are intentionally indistinguishable
409 DEFAULT_DEPARTMENT_DELETE_FORBIDDENDepartment is the company defaultMake another department the default first
409 DEPARTMENT_HAS_SUBDEPARTMENTSDepartment still has childrenDelete or move sub-departments first

State conflicts are not permission errors; adding scopes will not clear a 409.

Frequently asked questions

Does deleting a department require a request body?

No. The endpoint takes no request body; the department is identified by the id path parameter.

What happens if I try to delete the default department?

The API returns 409 DEFAULT_DEPARTMENT_DELETE_FORBIDDEN. The company's default department can never be deleted; make another department the default first, then retry.

What if the department still has sub-departments?

The API returns 409 DEPARTMENT_HAS_SUBDEPARTMENTS. Delete or move the sub-departments first, then retry.

How do I know whether the deletion succeeded when I get a 202?

Poll operation.url until status is SUCCEEDED or FAILED. A status of UNKNOWN means no result arrived within 15 minutes; the change may or may not have been applied, so read the resource to check. A result that arrives later still changes the status to SUCCEEDED or FAILED.

Is it safe to retry the DELETE?

Yes, provided the retry carries the same Idempotency-Key and repeats the identical request. Each key lives for 24 hours per API key; a matching retry inside that window returns the first response again, marked with the Idempotent-Replayed header. Pair the key with a different request and you get 422 IDEMPOTENCY_KEY_REUSED; send a retry while the original is still in flight and you get 409 IDEMPOTENCY_REQUEST_IN_PROGRESS.

Why does a 404 sometimes mean the department exists?

404 RESOURCE_NOT_FOUND is returned both when the department does not exist and when it is outside the key's data reach. The two cases are deliberately indistinguishable, so check the id and the key's visibility before concluding the department is gone.

curl -X DELETE "$API_BASE_URL/v2/departments/string" \
  -H "Authorization: Bearer "
{
  "operation": {
    "id": "pa_3f1c2b9e-8a47-4d5e-9c61-2b7f0e4a1d93",
    "status": "PENDING",
    "url": "/v2/operations/pa_3f1c2b9e-8a47-4d5e-9c61-2b7f0e4a1d93"
  }
}
{
  "error": {
    "code": "VALIDATION_ERROR",
    "name": "ValidationError",
    "description": "Example post details.",
    "resolution": "Fix the fields listed in details.fields and send the request again.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
    "details": {
      "fields": [
        {
          "path": "startDate",
          "message": "\"startDate\" is required"
        }
      ]
    }
  }
}
{
  "error": {
    "code": "UNAUTHORIZED",
    "name": "Unauthorized",
    "description": "Example post details.",
    "resolution": "Send a valid API key in the Authorization header as Bearer followed by the full vt_live_ key.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
  }
}
{
  "error": {
    "code": "INSUFFICIENT_SCOPE",
    "name": "InsufficientScope",
    "description": "Example post details.",
    "resolution": "Add the scopes listed in details.requiredScopes to the key in the OnlyLeave dashboard.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
    "details": {
      "requiredScopes": [
        "leaves:write"
      ]
    }
  }
}
{
  "error": {
    "code": "RESOURCE_NOT_FOUND",
    "name": "ResourceNotFound",
    "description": "Example post details.",
    "resolution": "Check the ID of the resource named in details.resource and that the key owner can see it.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
  }
}
{
  "error": {
    "code": "CONFLICT",
    "name": "Conflict",
    "description": "Example post details.",
    "resolution": "Read the resource again and retry with up-to-date values.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
  }
}
{
  "error": {
    "code": "LEAVE_NEGATIVE_BALANCE",
    "name": "LeaveNegativeBalance",
    "description": "Example post details.",
    "resolution": "Shorten the leave, use a different leave type, or ask an admin to adjust the quota.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
  }
}
{
  "error": {
    "code": "RATE_LIMITED",
    "name": "RateLimited",
    "description": "Example post details.",
    "resolution": "Wait a few seconds and retry, using the same Idempotency-Key for writes.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
  }
}
{
  "error": {
    "code": "INTERNAL_ERROR",
    "name": "InternalError",
    "description": "Example post details.",
    "resolution": "Retry later, and if it keeps happening, contact OnlyLeave support and quote the requestId.",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
  }
}

id

FieldTypeRequiredDescriptionConstraints
idstringYesresource ID.

Idempotency-Key

FieldTypeRequiredDescriptionConstraints
Idempotency-KeystringNooptional key (1 to 255 printable ASCII characters) that makes a write request safe to retry. Keys are retained for 24 hours per API key. Retrying with the same key and the same request within those 24 hours replays the original response (with the Idempotent-Replayed header). Reusing the key with a different request returns 422 IDEMPOTENCY_KEY_REUSED ; retrying while the first request is still running returns 409 IDEMPOTENCY_REQUEST_IN_PROGRESS ; an invalid key returns 400 INVALID_IDEMPOTENCY_KEY .1 <= length <= 255

X-On-Behalf-Of

FieldTypeRequiredDescriptionConstraints
X-On-Behalf-OfstringNoID of the user the write is performed for. Requires the users:act-on-behalf scope, and the key owner must manage the target user; otherwise the request fails with 403 ON_BEHALF_OF_NOT_ALLOWED . Ignored on GET requests.

response

FieldTypeRequiredDescriptionConstraints
responseobjectReturned with HTTP 202 when a write operation is still running, or succeeded but its result could not be read back yet. The Location header carries the same URL as operation.url . Poll it until the status is SUCCEEDED or FAILED .
response.operationobjectYes
response.operation.idstringYes
response.operation.statusstringYesoperation status. UNKNOWN means no result arrived within 15 minutes; the change may or may not have been applied, so read the resource to check. A result that arrives later still changes it to SUCCEEDED or FAILED.Allowed values: "PENDING" | "SUCCEEDED" | "FAILED" | "UNKNOWN"
response.operation.urlstringYes

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo

response

FieldTypeRequiredDescriptionConstraints
responseobjectError envelope returned by every endpoint, including authentication and rate limit errors. error.code is one of the codes in x-error-catalog .
response.errorobjectYes
response.error.codestringYesstable machine-readable error code
response.error.namestringYesshort human-readable error name
response.error.descriptionstringYeswhat went wrong
response.error.resolutionstringYeshow to fix the problem
response.error.requestIdstringYesrequest ID, include it when contacting support
response.error.detailsobjectNooptional additional information specific to the error code
response.error.details.[key: string]anyNo