Drupal Trash Module: The Ultimate Solution for Managing Deleted Content

  • Profile picture for user Manpreet Singh
Drupal Trash Module: The Ultimate Solution for Managing Deleted Content

No doubt, Content management in Drupal is exceptional. However, there have been instances of accidental deletion of taxonomy terms, nodes, and other entities, which is a major challenge. The trash module implements a soft delete mechanism. This allows administrators to restore the deleted content without relying on database rollbacks and backups.

Let's understand the technical exploration of the module, such as installation, Configuration, database architecture, API integrations, and best practices.

How Does The Trash Module Work?

The Drupal Trash Module helps prevent immediate entity deletions by redirecting them to a dedicated trash bin. Rather than permanently eliminating the records, it marks them as “trashed” and retains their metadata for a customizable period, allowing for recovery.

Technical Workflow

  1. Interception of Deletion Requests: The module integrates with Drupal’s entity deletion process and overrides the default functionality.        
  2. Storage of Deleted Content References: Rather than deleting data, it stores the entity records in the trash table.      
  3. Restoration Mechanism: You can restore the deleted entities by recreating them with their original data.
  4. Automatic Purging: A manual trigger or even a cron job can permanently delete trashed items based on a defined retention period.  

Installation & Setup

These are the steps you should follow to install and enable the Trash module.

  • Step 1: Download and Enable the Module

Using Composer:
composer requires drupal/trash
Or, manually download the module from Drupal.org and place it in modules/contrib/.

Enable the module via Drush:
Drush en trash -y

Alternatively, enable it through the Extend page in the Drupal admin panel.

  • Step 2: Set Up Permissions

Give roles under People>Permissions for: 

  1. Assessing trashed content 
  2. Restoring deleted items 
  3. Deleting the content permanently

Database Structure & API Integration 

Database Schema

The module creates a dedicated trash table to keep the metadata of deleted entities. Key fields include:

  • id (Primary Key)
  • entity_id (ID of the deleted entity)
  • entity_type (Type of entity, e.g., node, taxonomy_term)
  • deleted_by (User ID of the person who deleted the content)
  • deleted_timestamp (Time of deletion)
  • original_data (Serialized data of the deleted entity)

API Hooks and Services 

The module also exposes hooks and services for developers to expand functionality:

  1. Hook to Customize Soft Delete Behavior
function mymodule_trash_predelete(TrashEntityInterface $entity) {
\Drupal::logger('mymodule')->notice('Entity @type with ID @id moved to trash.', [
'@type' => $entity->getEntityTypeId(),
'@id' => $entity->id(),
]);
}
  1. Programmatic Recovery of Deleted Content
use Drupal\trash\TrashManager;
$trash_manager = \Drupal::service('trash.manager');
$trash_manager->restore($entity_id, 'node');
  1. Scheduled Auto-Purge Implementation 
function mymodule_cron() {
\Drupal::service('trash.manager')->purgeExpiredItems();
}

Using The Trash Module

  1. Viewing Deleted Content

Access the trash bin under Admin > Content > Trash.

  1. Restoring Deleted Items

Select the content items to restore.
Click Restore, and the entities will be back with their original data.

  1. Configuring Automatic Purging

Navigate to Configuration> Trash Settings.
Set the retention period for deleted content (e.g., permanently deleted after 30 days).

What Are the Advantages of Trash Modules?

We have listed down a few benefits of using trash modules in Drupal.

  • It helps prevent losing data permanently
  • It enhances the site administration while allowing granular control recovery.
  • It increases security and compliance by restricting permanent deletions to authorized users.
  • Streamlines content management by minimizing reliance on database rollbacks.

Final Thoughts 

The Trash Module is a perfect solution for recovering and managing deleted content in Drupal. It ensures that data is not permanently lost. By leveraging soft delete functionality, API support, role-based permissions, and automated purging, it helps administrators maintain better data management and efficient workflows.

On the other hand, for the developers, the module is great to offer a structured database schema, and flexible services, allowing personalization to adapt the workflows. Whether you are an administrator, developer, or content editor, the Trash module is an important addition to any Drupal setup.   

For developers, the module provides hooks, a structured database schema, and flexible services, allowing customization to fit specific workflows. Whether you're an administrator, content editor, or developer, the Trash module is a crucial addition to any Drupal setup.


Content provided by Laxmi Narayan Webworks Pvt Ltd. Thanks to Raman Kumar. 

LN Web Works is a dynamic Drupal development company in India, delivering innovative web and mobile app solutions that drive business success. With over a decade of experience, we empower organizations across diverse industries with tailored, client-focused IT services.

Image Attribution Disclaimer: At The Drop Times (TDT), we are committed to properly crediting photographers whose images appear in our content. Many of the images we use come from event organizers, interviewees, or publicly shared galleries under CC BY-SA licenses. However, some images may come from personal collections where metadata is lost, making proper attribution challenging.

Our purpose in using these images is to highlight Drupal, its events, and its contributors—not for commercial gain. If you recognize an image on our platform that is uncredited or incorrectly attributed, we encourage you to reach out to us at #thedroptimes channel on Drupal Slack.

We value the work of visual storytellers and appreciate your help in ensuring fair attribution. Thank you for supporting open-source collaboration!

Note: The vision of this web portal is to help promote news and stories around the Drupal community and promote and celebrate the people and organizations in the community. We strive to create and distribute our content based on these content policy. If you see any omission/variation on this please reach out to us at #thedroptimes channel on Drupal Slack and we will try to address the issue as best we can.

Advertisement Here

Related Organizations

Related People

Advertisement Here

Upcoming Events

Latest Opportunities

Advertisement Here