Delete a department
curl --request DELETE \
--url http://localhost:2000/department/{departmentId} \
--header 'Authorization: Bearer <token>'import requests
url = "http://localhost:2000/department/{departmentId}"
headers = {"Authorization": "Bearer <token>"}
response = requests.delete(url, headers=headers)
print(response.text)const options = {method: 'DELETE', headers: {Authorization: 'Bearer <token>'}};
fetch('http://localhost:2000/department/{departmentId}', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_PORT => "2000",
CURLOPT_URL => "http://localhost:2000/department/{departmentId}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://localhost:2000/department/{departmentId}"
req, _ := http.NewRequest("DELETE", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.delete("http://localhost:2000/department/{departmentId}")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("http://localhost:2000/department/{departmentId}")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Delete.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"message": "Successfully deleted Engineering department",
"data": {
"deletedDepartment": {
"_id": "64ef3c29f9a1c27e1b2c3a4d",
"name": "Engineering",
"code": "ENG-001",
"description": "Handles all software engineering operations"
}
}
}Department Management
Delete Department
Admin-only endpoint to delete a department by its ID. Returns the deleted department’s basic details if successful.
DELETE
/
department
/
{departmentId}
Delete a department
curl --request DELETE \
--url http://localhost:2000/department/{departmentId} \
--header 'Authorization: Bearer <token>'import requests
url = "http://localhost:2000/department/{departmentId}"
headers = {"Authorization": "Bearer <token>"}
response = requests.delete(url, headers=headers)
print(response.text)const options = {method: 'DELETE', headers: {Authorization: 'Bearer <token>'}};
fetch('http://localhost:2000/department/{departmentId}', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_PORT => "2000",
CURLOPT_URL => "http://localhost:2000/department/{departmentId}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "http://localhost:2000/department/{departmentId}"
req, _ := http.NewRequest("DELETE", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.delete("http://localhost:2000/department/{departmentId}")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("http://localhost:2000/department/{departmentId}")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Delete.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"message": "Successfully deleted Engineering department",
"data": {
"deletedDepartment": {
"_id": "64ef3c29f9a1c27e1b2c3a4d",
"name": "Engineering",
"code": "ENG-001",
"description": "Handles all software engineering operations"
}
}
}Admin-only endpoint to delete a department by its ID. Returns the deleted department’s basic details if successful.
Request
Headers
| Name | Type | Required | Description |
|---|---|---|---|
| Authorization | string | Yes | Bearer token |
Path Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| departmentId | string | Yes | Department ID to delete |
Response
200 OK - Successfully deleted department
{
"message": "Successfully deleted Engineering department",
"data": {
"deletedDepartment": {
"_id": "64b7f2b3e4b0a5d3f9c54321",
"name": "Engineering",
"description": "Software development and technical operations",
"parentId": null,
"deletedAt": "2024-01-16T15:30:00.000Z"
}
}
}
400 Bad Request
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Department does not exist or has active users"
}
}
401 Unauthorized
{
"error": {
"code": "UNAUTHORIZED",
"message": "Missing or invalid token or insufficient permissions"
}
}
500 Internal Server Error
{
"error": {
"code": "SERVER_ERROR",
"message": "Internal server error"
}
}
Example
curl -X DELETE 'http://localhost:2000/department/64b7f2b3e4b0a5d3f9c54321' \
-H 'Authorization: Bearer your-jwt-token'
Notes
- This is an admin-only endpoint - requires administrative privileges
- Department ID must be a valid MongoDB ObjectId
- Deletion is permanent - cannot be undone
- Department must be empty (no assigned users) to be deleted
- All sub-departments must be deleted first
- The response includes the deleted department’s details
deletedAttimestamp is added to track when deletion occurred
Prerequisites for Deletion
Before deleting a department, ensure:- No Active Users: All users must be reassigned to other departments
- No Sub-departments: All child departments must be deleted first
- No Dependencies: No active workflows depend on this department
Deletion Workflow
- Check Users: Verify no users are assigned to the department
- Handle Sub-departments: Delete or move all child departments
- Reassign Users: Move any remaining users to other departments
- Delete Department: Call this endpoint to remove the department
Error Scenarios
Department has users
{
"error": {
"code": "DEPARTMENT_HAS_USERS",
"message": "Cannot delete department with assigned users"
}
}
Department has sub-departments
{
"error": {
"code": "DEPARTMENT_HAS_CHILDREN",
"message": "Cannot delete department with sub-departments"
}
}
Impact Analysis
Before Deletion
- Check user assignments
- Verify sub-department status
- Review dependent systems
After Deletion
- Department is permanently removed
- Historical data may reference deleted department
- Users cannot be assigned to deleted department
Best Practices
- Backup Data: Export department data before deletion
- User Communication: Notify affected users of changes
- Gradual Process: Use deprecation before deletion when possible
- Audit Trail: Document deletion reasons and approvals
- Testing: Test deletion process in non-production environment
Safety Considerations
- Irreversible Action: Confirm before proceeding
- Data Integrity: Ensure no broken references
- User Impact: Minimize disruption to users
- Compliance: Follow organizational deletion policies
Alternative Approaches
Instead of deletion, consider:- Archiving: Mark department as inactive
- Renaming: Repurpose for new use
- Merging: Combine with another department
Authorizations
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
Path Parameters
Unique identifier of the department to delete
Was this page helpful?