Skip to content

[🐛 Bug]: Selenium created chrome profile directories are not getting deleted after driver.quit #17518

@rummitta

Description

@rummitta

Description

Hi Team,
driver.quit should ideally delete the selenium created chrome profile directories after quitting the driver. However, they are getting piled up in chrome node. We tried to delete using java delete but since the directories are created by seluser, our java process user does not have access to delete them. We even tried adding these config at hub as well as node level.

  • SE_ENABLE_BROWSER_LEFTOVERS_CLEANUP=true -- For cleanup enable
  • SE_BROWSER_LEFTOVERS_INTERVAL_SECS=300 -- Cleanup in every 5mins
  • SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS=0 -- Delete temporary file more than 1 day after browser closes
  • SE_BROWSER_LEFTOVERS_PROCESSES_SECS=3600
    Still the folders are not getting deleted.
Image

Regards,
Rumki

Reproducible Code

public static void cleanupOldChromeProfiles() {
        try {
            // Use /tmp/selenium as the base directory for Chrome profiles
            String baseDir = "/home/seluser/Downloads";
            Path tempPath = Paths.get(baseDir);

            if (!Files.exists(tempPath)) {
                log.debug("Chrome profiles directory does not exist: {}", baseDir);
                return;
            }

            long oneDayAgo = System.currentTimeMillis() - (24 * 60 * 60 * 1000);

            Files.list(tempPath)
                .filter(path -> path.getFileName().toString().startsWith("chrome_profile_"))
                .filter(path -> {
                    try {
                        long lastModified = Files.getLastModifiedTime(path).toMillis();
                        return lastModified < oneDayAgo;
                    } catch (IOException e) {
                        return false;
                    }
                })
                .forEach(path -> {
                    try {
                        log.info("Cleaning up old Chrome profile: {}", path);
                        Files.walk(path)
                            .sorted(Comparator.reverseOrder())
                            .forEach(p -> {
                                try {
                                    // Check and set file permissions before deletion
                                    if (Files.exists(p) && !Files.isDirectory(p)) {
                                        if (!Files.isWritable(p)) {
                                            log.debug("File is not writable: {}", p);
                                            // Try to set writable permission
                                            File file = p.toFile();
                                            boolean writable = file.setWritable(true);
                                            boolean readable = file.setReadable(true);
                                            log.debug("Set writable={}, readable={} for: {}", writable, readable, p);
                                        }
                                    }
                                    Files.deleteIfExists(p);
                                } catch (IOException e) {
                                    log.debug("Could not delete: {}", p);
                                }
                            });
                    } catch (Exception e) {
                        log.debug("Could not cleanup old profile: {}", path);
                    }
                });
        } catch (Exception e) {
            log.warn("Error during old Chrome profiles cleanup: {}", e.getMessage());
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions