Digital Cleanup Day with Box API

Olga Stefaniuk
Box Developer Blog
Published in
4 min readFeb 21, 2024
Photo by Taylor Vick on Unsplash

In today’s digital landscape, efficient file management is crucial for businesses. With the exponential volume of data being generated, it’s essential to have streamlined processes in place for organizing and disposing of outdated files and folders.

16th March 2024 is the Digital Cleanup Day 📂 🌍 🌱

The Digital Cleanup Day is dedicated to organizing and decluttering our digital lives. It’s an opportunity to clean up our digital devices, delete unnecessary files on content clouds, organize folders, update software, and review online accounts for security purposes. Additionally, Kevin Guerin, the initiator of this project, underlines that the initiative aims to “raise awareness of the environmental impact of IT through action by cleaning data, repairing and reusing old equipment, and recycling anything not usable anymore.” Celebrating Digital Cleanup Day helps to improve productivity by reducing digital clutter, enhances cybersecurity by removing outdated or unused accounts and data, promotes a healthier relationship with technology by encouraging mindfulness in our digital habits, and finally advocates for sustainability.

The Digital Cleanup Day, source: https://www.digitalcleanupday.org/home/resources-and-materials

Box API offers significant advantages when optimizing organizational efficiency regarding document lifecycle management practices.

In this post, we’re going to explore the possibilities of working with the Box TS SDK to manage content at scale. You can use the Box TS SDK on both the backend and frontend sides. Check out my previous guides for getting started and authenticating the Box app with Box TS SKD:

⚠️ Box TS SDK is still in beta phase.

List trashed items

Firstly, the Box API comes with an endpoint that returns all items that are stored in the trash folder. By using the TS SDK, you can easily get a list of trashed items:

await client.trashedItems.getTrashedItems();

This endpoint also accepts a sort parameter, so you can get items sorted by name, date, or size. Check out our documentation page for more details, parameters, and snippets in different programming languages. Here is an example response returning two trashed folders.

{
"total_count": 2,
"entries": [
{
"type": "folder",
"id": "234567899",
"sequence_id": "1",
"etag": "1",
"name": "My folder"
},
{
"type": "folder",
"id": "234567891",
"sequence_id": "1",
"etag": "1",
"name": "Marketing folder"
}
],
"offset": 0,
"limit": 100,
"order": [
{
"by": "type",
"direction": "ASC"
},
{
"by": "size",
"direction": "ASC"
}
]
}

Alternatively, you can use Box CLI and box trash command which lists all items in trash.

Webhooks

Box has a range of webhooks connected to the content lifecycle, including files and folders being trashed, deleted, or restored:

FILE.TRASHED,FILE.DELETED,FILE.RESTORED ,

FOLDER.TRASHED,FOLDER.DELETED, FOLDER.RESTORED.

Thanks to this, you can trigger actions based on user behaviour related to content management actions. If you want to learn more about webhooks in Box Platform, read Rui Barbosa’s blog post on this topic:

Searching and cleaning up files and folders 🗑️

Actions you can perform on Box files related to disposing are delete file, get trashed file, restore file, and permanently remove file. To perform these actions, you can use the Box TS SDK:


await client.files.deleteFileById(file.id);

await client.trashedFiles.getTrashedFileById(file.id);

await client.trashedFiles.restoreFileFromTrash(file.id);

await client.trashedFiles.deleteTrashedFileById(file.id);

The same actions can be performed on folders:

await client.folders.deleteFolderById(folder.id);

await client.trashedFolders.getTrashedFolderById(folder.id);

await client.trashedFolders.restoreFolderFromTrash(folder.id);

await client.trashedFolders.deleteTrashedFolderById(folder.id);

Thanks to the search endpoint, you can filter existing content by created_at_range , updated_at_range, and size_range. Here is a TS SDK snippet for searching content that was updated earlier than a given date and includes a specific keyword:

const deleteSearchedFiles = async (keyword, dates) => {
let searchResult = await client.search.searchForContent({
query: keyword,
updatedAtRange: dates
});
searchResult.entries.forEach(el => {
deleteFiles(search)
})
}

const deleteFiles = async (items) => {
await items.entries.forEach(el => {
if (el.type === 'file') {
client.files.deleteFileById(el.id);
}
})
}

deleteSearchedFiles('keyword', ['', '2023-03-16T13:35:01-07:00'])

The provided date should be in RFC3339 format, so you might use new Date().toISOString() and next convert it using this function.

Digital spring-cleaning 🌱

Digital Cleanup Day is an important initiative that raises awareness about the significance of maintaining a clean and organized digital environment. Everyone has the power to make a difference on Digital Cleanup Day by taking small steps towards tidying up their digital space, ultimately leading to a cleaner and more sustainable online world for all.

This topic certainly deserves more visibility, so share this post within your organization. Check for local initiatives and give your old digital equipment a second life. Let’s do some digital spring cleaning on our devices and content clouds.

Follow for more tutorials and Box Platform updates!

Join our Box Developer Community for support and knowledge sharing!

Cheers!

--

--