How to refresh recycle bin after calling [IFileOperation.DeleteItem] or [SHFileOperation]

iamoon-6427 60 Reputation points
2026-07-04T22:47:13.9133333+00:00

INFO

  • OS Windows 11 profession, 25H2, 26200.8655
  • Visual Studio latest with latest Windows SDK, etc

DETAILS

  • I have tried to implement a cmd line tool to move specific items to recycle bin on Windows.

After successfully calling IFileOperation -> DeleteItem or the old one SHFileOperation, it did perform the remove action.

However, the recycle bin on my desktop still show an empty icon, it did not refresh its state only until I open it in the file explorer (double click the icon on desktop).

When it's not refreshed yet, right click on it, showing the menu, option empty recycle bin is not clickable, same until manually open it.

I have tried:

  • reboot my PC
  • call SHChangeNotify(SHCNE_UPDATEITEM...) or SHChangeNotify(SHCNE_ALLEVENTS)
  • check

But none of them work.

I doubt whether I miss something here, but cannot find the resolution...

If needed, I can provide my code here or on github, but the logic is basically the same as the HRESULT DeleteSampleFiles(IShellItem *psiSrc, IShellItem *psiDst) of Microsoft github example, except that add below code:

HRESULT DeleteSampleFiles(IShellItem* psiSrc, IShellItem* psiDst)
{
    IFileOperation* pfo;
    HRESULT hr = CreateAndInitializeFileOperation(IID_PPV_ARGS(&pfo));
    if (SUCCEEDED(hr))
    {
        // below line is what I add, let delete folders stay in recycle bin
        // instead of delete permanently
        hr = pfo->SetOperationFlags(FOFX_RECYCLEONDELETE);
        if (SUCCEEDED(hr))
        {
            hr = pfo->DeleteItem(psiSrc, NULL);
            if (SUCCEEDED(hr))
            {
                hr = pfo->DeleteItem(psiDst, NULL);
                if (SUCCEEDED(hr))
                {
                    hr = pfo->PerformOperations();
                }
            }

        }
        pfo->Release();
    }
    return hr;
}

Thanks a lot if someone can help!

Windows development | Windows API - Win32
0 comments No comments

Answer accepted by question author

RLWA32 52,676 Reputation points
2026-07-05T07:17:24.17+00:00

I reproduced your observations on Win10 22H2. The following caused the desktop icon of the Recycle bin to refresh after an item was recycled from a console application. For example purposes all error checking is omitted.

int wmain(int argc, wchar_t *argv[])
{
    auto hr = CoInitialize(nullptr);
    {
        CComPtr<IShellItem> psibin, psifile;
        CComPtr<IFileOperation> pfo;
        CComPtr<IShellView> psv;

        // Fully qualified path of file to be recycled
        hr = SHCreateItemFromParsingName(argv[1], nullptr, IID_PPV_ARGS(&psifile));
        hr = SHGetKnownFolderItem(FOLDERID_RecycleBinFolder, KF_FLAG_DEFAULT, NULL, IID_PPV_ARGS(&psibin));
        hr = psibin->BindToHandler(nullptr, BHID_SFViewObject, IID_PPV_ARGS(&psv));
        hr = pfo.CoCreateInstance(CLSID_FileOperation, nullptr, CLSCTX_INPROC_SERVER);

        hr = pfo->SetOperationFlags(FOFX_RECYCLEONDELETE);
        hr = pfo->DeleteItem(psifile, nullptr);
        hr = pfo->PerformOperations();
        hr = psv->Refresh();
    }

    CoUninitialize();

    return 0;
}

I wasn't able to test this code on Win 11.

Was this answer helpful?

2 people found this answer helpful.

Answer accepted by question author

Taki Ly (WICLOUD CORPORATION) 2,815 Reputation points Microsoft External Staff Moderator
2026-07-06T04:01:51+00:00

Hello @iamoon-6427 ,

After looking into this further, I would point you to RLWA32's answer, which appears to be the reliable approach. I tested this on Windows 11 25H2. Binding to the Recycle Bin's view object and calling IShellView::Refresh() after PerformOperations() did update the desktop icon from empty to full, exactly as RLWA32 described for Win10 22H2:

CComPtr<IShellItem> psibin;
CComPtr<IShellView> psv;
SHGetKnownFolderItem(FOLDERID_RecycleBinFolder, KF_FLAG_DEFAULT, NULL, IID_PPV_ARGS(&psibin));
psibin->BindToHandler(nullptr, BHID_SFViewObject, IID_PPV_ARGS(&psv));
// ... after pfo->PerformOperations() ...
psv->Refresh();   // this is what updates the desktop icon

For completeness, on the same 25H2 machine I tested SHUpdateRecycleBinIcon and SHChangeNotify with SHCNE_UPDATEIMAGE, SHCNE_UPDATEITEM, and SHCNE_ASSOCCHANGED in isolation, and none of them updated the icon, only the IShellView::Refresh() call did. That is consistent with what you reported, since SHCNE_UPDATEITEM was already among the things you had tried.

Hope the information helps! If you found my response helpful or informative, I would really appreciate it if you could follow this instruction for your confirmation.

Thank you.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. iamoon-6427 60 Reputation points
    2026-07-06T19:07:25.9366667+00:00

    Thanks everyone! It's a bit strange that windows document not mentioning how to refresh the icon. The reason why I try SHChangeNotify is that source code of deletefiles in dotnet shows that we should call this after shell operations:

    NativeMethods.SHChangeNotify(
    SHChangeEventTypes.SHCNE_DISKEVENTS,SHChangeEventParameterFlags.SHCNF_DWORD, IntPtr.Zero, IntPtr.Zero
    )
    

    Not seeing any other section related to icon refresh...

    I try to reimplemnet it in C++, to test if it's useful (don't want to install dotnet just for a test). But just like I said before, this does not work.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.