Identify and Eliminate Unused Assets

Identify and Eliminate Unused Assets

Enhance Performance by Removing Excess

Take control of your project's asset collection. Remove extraneous images and PDF files that are not utilized within your project, freeing up valuable storage space and improving overall organization.

Our goal is to figure out how to remove unnecessary assets from the project, such as photos and PDFs.

The delete_unused_assets function accepts two parameters:

  1. assets_folder (the folder where the assets are stored) and project_folder (the root folder of the project against which the assets will be checked).

  2. If the bin directory does not already exist, the code builds it.

  3. It then obtains a list of all files in the assets_folder as well as a list of all image and PDF files in the project_folder.

  4. The unused assets are then identified by determining the set difference between the asset files and the project files.

Lets breakdown check how it works:

  1. It loads the required modules: 'os', 're', and 'shutil'.

  2. The 'BIN_DIRECTORY' constant variable is declared as ".bin". This is the directory into which the unused assets will be relocated.

  3. The 'delete_unused' method has three parameters: assets_folder, file_target_path, and ignore_folder. This function will relocate any assets that are no longer in use to the bin directory.

  4. The first step inside the 'delete_unused' method is to create the bin directory if it does not already exist.

  5. 'get_all_files_in_folder' is defined within 'delete_unused'. It searches the provided directory and its subdirectories recursively for all file paths. It ignores folders that begin with a dot or are specified in the 'ignore_folder' argument.

  6. The function gets the file paths of assets in the 'assets_folder' and target files in the 'file_target_path'.

  7. It then iterates over each target file, looking for asset names inside the file content with regular expressions. If a match is detected, the asset name is added to the list of 'found_terms'.

  8. After analyzing all of the target files, the script uses the set difference operation to identify the unused assets by subtracting the 'found_terms' from the 'target_assets_names'.

  9. It uses 'shutil.move' to relocate each unwanted asset to the bin directory. Finally, the 'delete_unused' function is called with the following parameters:

    "assets" for the 'assets_folder', "html" for the 'file_target_path', and the 'ignore_folders' list. points to verify the provided code assume that the assets and target files are in the same directory as the script or have the right relative paths.

The Python script attempts to eliminate unused assets from a given folder using regular expressions and file search techniques. It uses the constant variable ".bin" to identify the target path and imports modules such as os, re, and shutil. The delete_unused method moves unused assets to the bin directory using regular expressions and file search techniques.

If there is no bin directory, the script creates one and retrieves file paths for assets and target files. It finds asset names using regular expressions and identifies underutilized assets by subtracting found_terms from target_assets_names. The script uses shutil.move to relocate each useless asset to the bin directory.

Caution: use caution while running this code because it performs file modification operations, which can have unintended consequences if used incorrectly. This code is supplied for guidance purposes only and should be used at your own risk. Use caution, double-check paths, make backups, adapt to your project structure, and get professional help if you are unsure about asset removal or code execution.

Thank you for reading our blog. Our top priority is your success and satisfaction. We are ready to assist with any questions or additional help.

This code is contributed by our senior developer.

Warm regards,

ByteScrum Blog Team,

ByteScrum Technologies Private Limited! 🙏

Did you find this article valuable?

Support ByteScrum Technologies by becoming a sponsor. Any amount is appreciated!